Using WordPress ‘lost_password’ PHP action

The lost_password WordPress PHP action fires before the lost password form.

Usage

add_action('lost_password', 'your_custom_function');
function your_custom_function($errors) {
    // your custom code here
}

Parameters

  • $errors (WP_Error): A WP_Error object containing any errors generated by using invalid credentials. Note that the error object may not contain any errors.

More information

See WordPress Developer Resources: lost_password

Examples

Add a custom message to the lost password form

Add a custom message above the lost password form.

add_action('lost_password', 'add_custom_message');
function add_custom_message($errors) {
    _e('<p class="message">**Please enter your email address** to receive a link to create a new password.</p>', 'your-text-domain');
}

Log lost password form attempts

Log the number of times the lost password form has been displayed.

add_action('lost_password', 'log_lost_password_attempts');
function log_lost_password_attempts($errors) {
    $count = get_option('lost_password_count', 0);
    update_option('lost_password_count', ++$count);
}

Add a reCAPTCHA to the lost password form

Integrate Google reCAPTCHA with the lost password form.

add_action('lost_password', 'add_recaptcha_to_lost_password');
function add_recaptcha_to_lost_password($errors) {
    echo '<div class="g-recaptcha" data-sitekey="**your-recaptcha-site-key**"></div>';
}

Customize the lost password form title

Change the title of the lost password form.

add_action('lost_password', 'customize_lost_password_title');
function customize_lost_password_title($errors) {
    _e('<h2 class="form-title">**Forgot Your Password?**</h2>', 'your-text-domain');
}

Add a custom CSS class to the lost password form

Add a custom CSS class to the lost password form for additional styling.

add_action('lost_password', 'add_custom_css_class_to_lost_password');
function add_custom_css_class_to_lost_password($errors) {
    echo '<div class="custom-class">';
}