Only Named Administrators Can Manage Plugins, Themes and Users
Limits installing, activating, editing and deleting plugins and themes, updating WordPress and managing users to the administrator accounts you list by user ID, even if other accounts have the administrator role.
Opens WPIDE on
Code
// User IDs allowed to manage code and users.
$wpide_trusted_admins = array( 1 );
add_filter( 'map_meta_cap', function ( $caps, $cap, $user_id ) use ( $wpide_trusted_admins ) {
static $guarded = array( 'install_plugins', 'activate_plugins', 'deactivate_plugins', 'delete_plugins', 'update_plugins', 'edit_plugins', 'install_themes', 'switch_themes', 'delete_themes', 'update_themes', 'edit_themes', 'update_core', 'create_users', 'promote_users', 'delete_users', 'edit_users', 'remove_users' );
if ( ! in_array( $cap, $guarded, true ) || in_array( (int) $user_id, array_map( 'intval', $wpide_trusted_admins ), true ) ) {
return $caps;
}
static $any_trusted = null;
if ( null === $any_trusted ) {
$any_trusted = false;
foreach ( $wpide_trusted_admins as $id ) {
$trusted = get_userdata( (int) $id );
if ( $trusted && in_array( 'administrator', (array) $trusted->roles, true ) ) {
$any_trusted = true;
break;
}
}
}
return $any_trusted ? array( 'do_not_allow' ) : $caps;
}, 20, 3 );
Notes
Put your own user ID in the list first (find it in the address bar when editing your profile); if none of the listed IDs is an administrator, the snippet does nothing so you cannot lock yourself out.
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.