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

  • Kostas
    • Deputy CTO

    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

  • Nastia
    • Ex Staff

    Hello Direktorat Pusat Teknologi Informasi Telkom University

    I trust you are doing well!

    Thank you for posting in this thread, however, because this issue is resolved for the original author that has posted it, please create a new one. This way, the author will not be getting email notifications each time when there is a new post in the thread.

    You can open a new live chat with us if you need assistance with the mu-plugin that is shared in this thread.

    The current plugin shared in this thread is hiding only the Branda plugin from a sidebar.

    To hide other plugins please have a look at this blog post:
    https://theme4press.com/10-great-plugins-hide-sorts-wordpress-elements/

    Thank you

    Have a good day and take care!

    Cheers,
    Nastia