Skip to content

Instantly share code, notes, and snippets.

@tahmid-ul
Last active December 9, 2021 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tahmid-ul/2d35bb77ea8c0e59e59d9c862af69f70 to your computer and use it in GitHub Desktop.
Save tahmid-ul/2d35bb77ea8c0e59e59d9c862af69f70 to your computer and use it in GitHub Desktop.
add_action('fluentform_before_insert_submission', function ($insertData, $data, $form) {
if($form->id != 98) { // 23 is your form id. Change the 23 with your own login for ID
return;
}
$selected_login = \FluentForm\Framework\Helpers\ArrayHelper::get($data, 'input_radio');
if($selected_login == 'Login') {
$redirectUrl = home_url(); // You can change the redirect url after successful login
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_login'); // your form should have email field
$password = \FluentForm\Framework\Helpers\ArrayHelper::get($data, 'password_login'); // your form should have password field
if(!$email || !$password) {
wp_send_json_error(
'Please provide email and password'
);
}
$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' => 'Your are logged in, Please wait while you are redirecting'
]
]);
} else {
// password or user don't match
wp_send_json_error(
'Email / password is not correct'
);
}
}
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment