Halloween Sale 15% off Ends in See the plans
PHP

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.

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

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

  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.