Hide plugin from other admins

Please advise, looking for a way to allow only to one admin user to access the Ultimate Branding plugin settings and to hide the plugin from other users on the Plugins > Installed Plugins page as well

  • Konstantinos Xenos

    Hi DanH597 ,

    You can add the following code either in your functions.php ( preferably the child-themes ) or create a mu-plugin.

    To create a mu-plugin you simply create a folder if it doesn't exist called "mu-plugins" inside /wp-content/ directory, create a file named hide-branding.php inside your newly created mu-plugins folder and paste the following code.

    add_action( 'pre_current_active_plugins', 'hide_plugins_from_list_if_not_admin' );
    function hide_plugins_from_list_if_not_admin() {
    	if ( is_user_logged_in() ) {
    		$user = wp_get_current_user();
    		if ( '1' != $user->ID ) {
    			global $wp_list_table;
    
    			$plugins_list   = $wp_list_table->items;
    			$hidden_plugins = array( 'ultimate-branding/ultimate-branding.php' );
    
    			foreach ( $plugins_list as $key => $val ) {
    				if ( in_array( $key, $hidden_plugins ) ) {
    					unset( $wp_list_table->items[ $key ] );
    				}
    			}
    		}
    	}
    }
    
    add_action( 'admin_menu', 'hide_plugins_from_menu', 15 );
    function hide_plugins_from_menu() {
    	if ( is_user_logged_in() ) {
    		$user = wp_get_current_user();
    		if ( '1' != $user->ID ) {
    			$menu_items = array( 'branding' );
    
    			foreach ( $menu_items as $menu_item ) {
    				remove_menu_page( $menu_item );
    			}
    		}
    	}
    }

    Both functions support arrays so you can add more plugins if you like to the lists and don't forget to change the '1' into the ID of the admin you want to continue to have access.

    You can also see a full mu-plugin with this code and download it as well if it's easier at https://gist.github.com/wpmudev-sls/e46e1991c6e108cdfa4654a61bb041e9

    I hope this helps and tell me if you need further help!

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.