Halloween Sale 15% off Ends in See the plans
PHP

Remind Administrators to Change Old Passwords

Shows administrators a reminder when they have not changed their password for 180 days.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.