Add Password Reset Functionality for WordPress Admins

How to reset wordpress admin password; Wordpress admin password reset tutorial; Reset wordpress admin password via phpmyadmin; Customize wordpress admin password reset page; Wordpress admin password recovery options; Wordpress admin password reset email customization; How to change wordpress admin password; Wordpress admin password reset plugin; Wordpress admin password reset link not working; Wordpress admin password reset without email;

Explanation

This code adds a handy feature to your WordPress admin area, allowing you to reset your admin password directly from the dashboard. Here's how it works:

  • Admin Menu Link: A new menu item called "Password Reset" appears in your admin dashboard. Clicking it takes you to a page where you can reset your password.
  • Password Reset Form: On this page, there's a simple form with a button. When you click it, your password is reset to a new, randomly generated one.
  • Email Notification: After resetting, an email is sent to your registered email address with the new password. This ensures you can access your account with the new credentials.
  • Email Customization: The email you receive is customized to include your site name and a friendly message, making it clear what the email is about.

Remember, this feature is only accessible to users with the right permissions, ensuring that only authorized admins can reset passwords. It's a simple yet effective way to manage your admin credentials without needing to dive into more complex methods like using phpMyAdmin.

Code

<?php
// Function to add a custom password reset link for admins
function wp_dudecom_add_admin_password_reset_link() {
    add_menu_page(
        'Admin Password Reset', // Page title
        'Password Reset', // Menu title
        'manage_options', // Capability
        'wp-dudecom-password-reset', // Menu slug
        'wp_dudecom_admin_password_reset_page', // Callback function
        'dashicons-admin-network', // Icon
        100 // Position
    );
}
add_action('admin_menu', 'wp_dudecom_add_admin_password_reset_link');

// Callback function to display the password reset form
function wp_dudecom_admin_password_reset_page() {
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }

    if (isset($_POST['wp_dudecom_reset_password'])) {
        check_admin_referer('wp_dudecom_reset_password_action', 'wp_dudecom_reset_password_nonce');

        $user_id = get_current_user_id();
        $new_password = wp_generate_password(12, true);

        wp_set_password($new_password, $user_id);

        echo '<div class="updated"><p>' . __('Password has been reset. Please check your email for the new password.') . '</p></div>';

        wp_mail(
            wp_get_current_user()->user_email,
            __('Your New Admin Password'),
            sprintf(__('Your new password is: %s'), $new_password)
        );
    }

    ?>
    <div class="wrap">
        <h1><?php _e('Admin Password Reset'); ?></h1>
        <form method="post" action="">
            <?php wp_nonce_field('wp_dudecom_reset_password_action', 'wp_dudecom_reset_password_nonce'); ?>
            <p><?php _e('Click the button below to reset your admin password. A new password will be sent to your email.'); ?></p>
            <p><input type="submit" name="wp_dudecom_reset_password" class="button-primary" value="<?php _e('Reset Password'); ?>" /></p>
        </form>
    </div>
    <?php
}

// Function to customize the password reset email
function wp_dudecom_customize_password_reset_email($message, $key, $user_login, $user_data) {
    $site_name = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
    $message = sprintf(__('Hello %s,'), $user_data->display_name) . "\r\n\r\n";
    $message .= __('You requested a password reset for your account on ') . $site_name . ".\r\n\r\n";
    $message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
    $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . "\r\n\r\n";
    $message .= __('If you did not request this, please ignore this email.') . "\r\n\r\n";
    $message .= __('Thanks!') . "\r\n";
    $message .= $site_name . "\r\n";

    return $message;
}
add_filter('retrieve_password_message', 'wp_dudecom_customize_password_reset_email', 10, 4);
?>

Instructions

To implement the password reset functionality for admins, follow these steps:

File Location: Add the code to your theme's functions.php file or a custom plugin file.

Prerequisites: Ensure you have administrator access to your WordPress site.

Implementation Steps:

  1. Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation files.
  2. Locate the File: Navigate to wp-content/themes/your-active-theme/functions.php or create a new custom plugin file in wp-content/plugins/.
  3. Edit the File: Open the functions.php file or your custom plugin file in a text editor.
  4. Add the Code: Copy and paste the provided code into the file. Ensure it's placed at the end of the file or in a logical section if using a plugin.
  5. Save Changes: Save the file and upload it back to your server if using FTP.
  6. Verify the Functionality: Log in to your WordPress admin dashboard. You should see a new menu item labeled "Password Reset" under the admin menu.
  7. Test the Password Reset: Click on "Password Reset" and follow the instructions to reset your password. Check your email for the new password.

By following these steps, you can easily add a password reset feature for admins in your WordPress site. If you need assistance with implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert help.