I have a recent issue for a particular code snippet I am trying to use in MainWP Code Snippets extension for one website, and it works fine when using it in WPCodeBox or in the functions.php file, but for some reason is not working properly when set via MainWP.
Here’s the snippet I’m trying to run:
/*
* Sets login form redirect action in Fluent Forms
*/
add_action(
"fluentform_before_insert_submission",
function ($insertData, $data, $form) {
if ($form->id != 5) {
// 23 is your form id. Change the 23 with your own login for ID
return;
}
$redirectUrl = "/account"; // You can change the redirect url after successful login
// if you have a field as refer_url as hidden field and value is: {http_referer} then
// We can use that as a redirect URL. We will redirect if it's the same domain
// If you want to redirect to a fixed URL then remove the next 3 lines
if (
!empty($data["refer_url"]) &&
strpos($data["refer_url"], site_url()) !== false
) {
$redirectUrl = $data["refer_url"];
}
if (get_current_user_id()) {
// user already registered
wp_send_json_success([
"result" => [
"redirectUrl" => $redirectUrl,
"message" =>
"Your are already logged in. Redirecting now...",
],
]);
}
$email = ("\FluentForm\Framework\Helpers\ArrayHelper::get")(
$data,
"email"
); // your form should have email field
$password = ("\FluentForm\Framework\Helpers\ArrayHelper::get")(
$data,
"password"
); // your form should have password field
if (!$email || !$password) {
wp_send_json_error(
[
"errors" => ["Please provide your email & password"],
],
423
);
}
$user = get_user_by_email($email);
if (
$user &&
wp_check_password($password, $user->user_pass, $user->ID)
) {
wp_clear_auth_cookie();
wp_set_current_user($user->ID);
wp_set_auth_cookie($user->ID);
/* user is not logged in.
* If you use wp_send_json_success the the submission will not be recorded
* If you remove the wp_send_json_success then it will record the data in fluentform
* in that case you should redirect the user on form submission settings
*/
wp_send_json_success([
"result" => [
"redirectUrl" => $redirectUrl,
"message" =>
"Successfully logged in. Please wait while you are redirected to your Account page.",
],
]);
} else {
// password or user don't match
wp_send_json_error(
[
"errors" => [
"Email and/or password is incorrect. Please verify your credentials and try again.",
],
],
423
);
}
},
10,
3
);
And when I run it in MainWP, when trying to use the form to login, I get the following PHP error:
Apr 09 23:38:33 [Mon Apr 10 06:38:33.639727 2023] [php:error] [pid 2300] [client 207.216.93.172:60434] PHP Fatal error: Uncaught Error: Class "FluentFormFrameworkHelpersArrayHelper" not found in /app/data/public/wp-content/plugins/mainwp-child/class/class-mainwp-utility.php(87) : eval()'d code:74\nStack trace:\n#0 /app/data/public/wp-includes/class-wp-hook.php(308): MainWP\\Child\\MainWP_Utility::{closure}()\n#1 /app/data/public/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters()\n#2 /app/data/public/wp-includes/plugin.php(517): WP_Hook->do_action()\n#3 /app/data/public/wp-content/plugins/fluentform/app/Modules/Form/FormHandler.php(113): do_action()\n#4 /app/data/public/wp-content/plugins/fluentform/app/Hooks/Ajax.php(16): FluentForm\\App\\Modules\\Form\\FormHandler->onSubmit()\n#5 /app/data/public/wp-includes/class-wp-hook.php(308): FluentForm\\Framework\\Foundation\\Application->{closure}()\n#6 /app/data/public/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters()\n#7 /app/data/public/wp-includes/plugin.php(517): WP_Hook->do_action()\n#8 /app/data/public/wp-admin/admin-ajax.php(203): do_action()\n#9 {main}\n thrown in /app/data/public/wp-content/plugins/mainwp-child/class/class-mainwp-utility.php(87) : eval()'d code on line 74, referer: https://www.staging.netcomtraining.org/account/
Again, this works fine when the code snippet runs from WPCodeBox or in the theme’s functions.php file, so I imagine there’s a processing issue when placed into the database from MainWP. Please help. Thank you.
I’m using both MainWP Dashboard and MainWP Child plugins at version 4.4.0.4.