Skip to content

Instantly share code, notes, and snippets.

@aimahdi
Last active July 8, 2024 08:48
Show Gist options
  • Save aimahdi/b674d925e3fa45af30ebeb1014011e49 to your computer and use it in GitHub Desktop.
Save aimahdi/b674d925e3fa45af30ebeb1014011e49 to your computer and use it in GitHub Desktop.
Login with User Name Or Email
add_action('fluentform/before_insert_submission', function ($insertData, $data, $form) {
if($form->id != 557) { // 557 is your form id. Change the 229 with your own login for ID
return;
}
$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...'
]
]);
}
$usernameOrEmail = \FluentForm\Framework\Helpers\ArrayHelper::get($data, 'user_name_or_email'); // your form should have username field
$password = \FluentForm\Framework\Helpers\ArrayHelper::get($data, 'password'); // your form should have password field
if(!$usernameOrEmail || !$password) {
wp_send_json_error('Please provide username and password');
}
$user = wp_authenticate($usernameOrEmail, $password);
if($user instanceof WP_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 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
*/
$redirectUrl = home_url();
wp_send_json_success([
'result' => [
'redirectUrl' => $redirectUrl,
'message' => 'You are logged in, Please wait while you are redirecting'
]
]);
} else {
// password or user don't match
wp_send_json_error('Username / password is not correct');
}
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment