20

How to Disable Password Reset in WordPress

Powered by WPCodeBox

Disable password reset functionality in WordPress. This guide explains how only users with administrator roles can change passwords from the admin area, enhancing WordPress security.


<?php 

// In admin area
if ( is_admin() ) {
	add_action( 'init', 'disable_password_fields', 10 );

	function disable_password_fields() {
		// if ( ! current_user_can( 'administrator' ) )
		$show_password_fields = add_filter( 'show_password_fields', '__return_false' );
	}
}

// Hide pass reset link on login screen
function remove_lostpassword_text ( $text ) {
	if ($text == 'Lost your password?'){$text = '';}
	return $text;
}
add_filter( 'gettext', 'remove_lostpassword_text' );

// Disable Password Reset URL & Redirect
function disable_lost_password() {
    if (isset( $_GET['action'] )){
        if ( in_array( $_GET['action'], array('lostpassword', 'retrievepassword') ) ) {
            wp_redirect( wp_login_url(), 301 );
            exit;
        }
    }
}
add_action( "login_init", "disable_lost_password" );

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.