lostpassword_post
Fires before errors are returned from a password reset request. Fires before password reset errors are returned, allowing modification of the WP_Error object.
add_action( 'lostpassword_post', $callback, 10, 1 );
Description
Fires after initial password reset checks but before errors are returned. Developers can add custom validation, modify error messages, or alter the error object before it's displayed to the user, ensuring robust password recovery processes.
Usage
add_action( 'lostpassword_post', 'your_function_name', 10, 1 );
Parameters
-
$errors(WP_Error) - A WP_Error object containing any errors generated by using invalid credentials.
Examples
<?php
/**
* Example function to hook into the 'lostpassword_post' action.
* This function could be used to add custom logging or modify the errors array
* after the default WordPress lost password process has run.
*
* @param WP_Error $errors A WP_Error object containing any errors generated by using invalid credentials.
*/
function my_custom_lostpassword_post_handler( WP_Error $errors ) {
// Check if there are any errors already.
if ( $errors->has_errors() ) {
// You could log the errors for debugging purposes.
error_log( 'Lost password process encountered errors: ' . print_r( $errors->get_error_messages(), true ) );
// You could also add a custom error message if a specific error code exists.
if ( $errors->has_error_code( 'invalidcombo' ) ) {
$errors->add( 'custom_invalid_user', __( 'We were unable to find an account with the details you provided. Please check your username or email and try again.', 'my-text-domain' ) );
}
} else {
// If no errors, you might want to log a successful (or attempted) password reset.
// Note: This hook fires *after* the email is sent, so it's more about the process completion.
// For actual success tracking, you might need to look at other hooks or application-level logic.
error_log( 'Lost password process completed without default errors.' );
// Example: Add a custom success message to the user's session or redirect page (though this hook doesn't directly control redirects).
// This would typically involve setting a transient or cookie that a subsequent page can read.
// set_transient( 'my_password_reset_attempted_' . get_current_user_id(), true, HOUR_IN_SECONDS );
}
// This is an action hook, so we don't need to return anything.
// However, if this were a filter, you would return the modified $errors object.
}
// Hook the function to the 'lostpassword_post' action.
// The third parameter '2' indicates that our function accepts two arguments:
// $errors (as provided by the hook).
add_action( 'lostpassword_post', 'my_custom_lostpassword_post_handler', 10, 1 );
?>
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/frontend-login-plus.php:3622
* @param WP_Error $errors A WP_Error object containing any errors generated
* by using invalid credentials.
*
* @since 4.4.0 Added the `$errors` parameter.
*
* @since 2.1.0
*/
do_action( 'lostpassword_post', $errors );
if ( $errors->has_errors() ) {
return $errors;
}
if ( ! $user_data ) {
$errors->add( 'invalidcombo', __( '<strong>ERROR</strong>: There is no account with that username or email address.', 'uncanny-learndash-toolkit' ) );