Remind Administrators to Change Old Passwords
Shows administrators a reminder when they have not changed their password for 180 days.
Opens WPIDE on
Code
// Days before the reminder appears.
$wpide_password_max_age = 180;
add_action( 'after_password_reset', function ( $user ) {
update_user_meta( $user->ID, 'wpide_password_changed', time() );
} );
add_action( 'profile_update', function ( $user_id, $old_user ) {
$user = get_userdata( $user_id );
if ( $user && $old_user && $user->user_pass !== $old_user->user_pass ) {
update_user_meta( $user_id, 'wpide_password_changed', time() );
}
}, 10, 2 );
add_action( 'admin_notices', function () use ( $wpide_password_max_age ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$user_id = get_current_user_id();
$changed = (int) get_user_meta( $user_id, 'wpide_password_changed', true );
if ( ! $changed ) {
update_user_meta( $user_id, 'wpide_password_changed', time() ); // Start counting from today.
return;
}
if ( time() - $changed > $wpide_password_max_age * DAY_IN_SECONDS ) {
printf(
'<div class="notice notice-warning"><p>You have not changed your password in over %1$d days. <a href="%2$s">Choose a new one</a>.</p></div>',
(int) $wpide_password_max_age,
esc_url( admin_url( 'profile.php#password' ) )
);
}
} );
Notes
The count starts the first time each administrator opens the dashboard after activation.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.