Remove menu in backend

Here is an plugin code, that will remove menu in backend.
It work fine, but how do I add the functionality so it won't affect the role Super Admin (somebody with access to the site network administration features and all other features).

<?php
/*
Plugin Name: Remove Settings and Posts Menu
Description: Just don't want my settings menu anymore and I don't want to write any posts - what's the big deal?
Version: 0.1
License: GPL
Author: Martin Morfjord
Author URI: https://premium.wpmudev.org/blog/how-to-remove-menus-from-the-wordpress-dashboard/
https://codex.wordpress.org/Function_Reference/remove_menu_page
*/

add_action( 'admin_menu', 'my_remove_menu_pages' );

function my_remove_menu_pages() {
//remove_menu_page( 'index.php' ); //Dashboard
//remove_menu_page( 'jetpack' ); //Jetpack*
//remove_menu_page( 'edit.php' ); //Posts
remove_menu_page( 'upload.php' ); //Media
//remove_menu_page( 'edit.php?post_type=page' ); //Pages
remove_menu_page( 'edit-comments.php' ); //Comments
remove_menu_page( 'themes.php' ); //Appearance
//remove_menu_page( 'plugins.php' ); //Plugins
//remove_menu_page( 'users.php' ); //Users
remove_menu_page( 'tools.php' ); //Tools
remove_menu_page( 'options-general.php' ); //Settings
remove_menu_page( 'link-manager.php' ); //Links
remove_submenu_page ( 'index.php', 'update-core.php' ); //Dashboard->Updates
remove_submenu_page ( 'index.php', 'my-sites.php' ); //Dashboard->my-sites
remove_submenu_page( 'themes.php', 'theme-editor.php' );
remove_submenu_page( 'themes.php', 'themes.php' );// Appearance-->Themes
remove_submenu_page( 'themes.php', 'customize.php' );
remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=post_tag' );
remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=category' );
//remove_submenu_page( 'edit.php', 'post-new.php' );
remove_submenu_page( 'themes.php', 'nav-menus.php' );
remove_submenu_page( 'themes.php', 'widgets.php' ); // Appearance-->Widgets
remove_submenu_page( 'themes.php', 'theme-editor.php' ); // Appearance-->Editor
//remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
//remove_submenu_page( 'plugins.php', 'plugin-install.php' );
//remove_submenu_page( 'users.php', 'users.php' );
//remove_submenu_page( 'users.php', 'user-new.php' );
//remove_submenu_page( 'upload.php', 'media-new.php' );
//remove_submenu_page ( 'options-general.php', 'options-general.php' ); // Settings->General
remove_submenu_page( 'options-general.php', 'options-writing.php' ); // Settings->writing
remove_submenu_page( 'options-general.php', 'options-discussion.php' );
remove_submenu_page( 'options-general.php', 'options-reading.php' ); // Settings->Reading
remove_submenu_page( 'options-general.php', 'options-discussion.php' ); //Settings->Discussion
remove_submenu_page( 'options-general.php', 'options-media.php' ); // Settings->Media
remove_submenu_page( 'options-general.php', 'options-privacy.php' ); // Settings->Privacy
remove_submenu_page( 'options-general.php', 'options-permalinks.php' );// Settings->permalinks
remove_submenu_page( 'index.php', 'update-core.php' );
remove_submenu_page( 'options-general.php', 'options-general.php?page=akismet-key-config' ); //akismet key

};

?>

  • Tyler Postle

    Hey morfjord,

    Hope you're doing well today!

    You would need to pull in user data and add an if statement at the beginning, like this:

    add_action( 'admin_menu', 'my_remove_menu_pages' );
    function my_remove_menu_pages() {
    
    $current_user = wp_get_current_user();
      if ($current_user->user_login!='super admin username goes here') {
    remove_menu_page( 'themes.php' ); //Appearance
    // ...all other pages
    }
    }

    Make sure to replace the "super admin username goes here" with the username of your super admin.

    Alternatively, you could just use this plugin: https://en-ca.wordpress.org/plugins/admin-menu-editor/

    It makes it really easy to manage admin menu visibility across all your roles. In your case you would want to hide from all except those users with a super admin only capability, like manage_network_options

    Hope that helps! Any further questions just let us know.

    Cheers,
    Tyler

  • Sajid

    Hi morfjord,
    Hope you are doing good today :slight_smile:

    There appears to be issue with remove_submenu_page to work (I tested that but could not make to work either). However, I found a following workaround to remove that menu.

    function nstrm_remove_admin_submenus() {
    	 global $submenu;
         // Appearance Menu
         unset($submenu['themes.php'][6]); // Customize
    }

    If the above don't work as it is, then position can be different for you so print_r or dump the $sumenu to get the exact index number.

    Hope that helps! Feel free to post a reply if you need further assistance :slight_smile:

    Best Regards,
    Sajid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.