Last active
April 9, 2025 10:09
-
-
Save aimahdi/6fc01cf7c0ccd73bd8ad38e578c69375 to your computer and use it in GitHub Desktop.
Add a lost password action in Fluent Forms
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 != 17) { // 17 is your form id. Change the 17 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...' | |
] | |
]); | |
} | |
$email = \FluentForm\Framework\Helpers\ArrayHelper::get($data, 'email'); // your form should have email field | |
if(!$email) { | |
wp_send_json_error([ | |
'errors' => ['Please provide email'] | |
], 423); | |
} | |
$user = get_user_by_email($email); | |
if($user ) { | |
$name = $user->user_login; | |
$email = $user->user_email; | |
$adt_rp_key = get_password_reset_key( $user ); | |
$user_login = $user->user_login; | |
$rp_link = '<a href="' . wp_login_url()."?action=rp&key=$adt_rp_key&login=" . rawurlencode($user_login) . '">Reset Link</a>'; | |
if ($name == "") $name = "There"; | |
$message = "Hi ".$name.",<br>"; | |
$message .= "Click here to resset the password for your account: <br>"; | |
$message .= $rp_link.'<br>'; | |
$subject = __("Your account on ".get_bloginfo( 'name')); | |
$headers = array(); | |
add_filter( 'wp_mail_content_type', function( $content_type ) {return 'text/html';}); | |
// $headers[] = 'From: Your company name <[email protected]>'."\r\n"; | |
wp_mail( $email, $subject, $message, $headers); | |
// Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578 | |
remove_filter( 'wp_mail_content_type', 'set_html_content_type' ); | |
wp_send_json_success([ | |
'result' => [ | |
'message' => 'Your are password reset link has been sent your email.' | |
] | |
]); | |
} else { | |
// password or user don't match | |
wp_send_json_error([ | |
'errors' => ['Email / 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
You are most welcome.