Last active
December 11, 2021 12:30
-
-
Save aimahdi/ea23c05be770a4bdeeaa3e5ccc38fc0d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('fluentform_before_insert_submission', function ($insertData, $data, $form) { | |
if($form->id != 229) { // 229 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...' | |
] | |
]); | |
} | |
$username = \FluentForm\Framework\Helpers\ArrayHelper::get($data, 'username'); // your form should have username field | |
$password = \FluentForm\Framework\Helpers\ArrayHelper::get($data, 'password'); // your form should have password field | |
if(!$username || !$password) { | |
wp_send_json_error([ | |
'errors' => ['Please provide username and password'] | |
], 423); | |
} | |
$user = wp_authenticate($username, $password); | |
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 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([ | |
'errors' => ['Username / password is not correct'] | |
], 423); | |
} | |
}, 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment