Warn When There Are Too Many Administrators
Shows a dashboard notice listing the administrator accounts when there are more than you expect, a prompt to downgrade old or unused ones.
Opens WPIDE on
Code
// Highest number of administrators you consider normal.
$wpide_max_admins = 3;
add_action( 'admin_notices', function () use ( $wpide_max_admins ) {
$screen = get_current_screen();
if ( ! $screen || 'dashboard' !== $screen->id || ! current_user_can( 'list_users' ) ) {
return;
}
$admins = get_users( array( 'role' => 'administrator', 'fields' => array( 'user_login' ), 'number' => 50 ) );
if ( count( $admins ) <= $wpide_max_admins ) {
return;
}
printf(
'<div class="notice notice-warning"><p><strong>%1$d administrator accounts:</strong> %2$s. Give people the lowest role they need. <a href="%3$s">Review administrators</a>.</p></div>',
count( $admins ),
esc_html( implode( ', ', wp_list_pluck( $admins, 'user_login' ) ) ),
esc_url( admin_url( 'users.php?role=administrator' ) )
);
} );
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.