WordPress Menu with logo
WordPress Snippets

Hide a WordPress Admin Menu Based on User Role

Sometimes there may be areas of the WordPress admin area that you do not want your users to have access to. For example, you may want editors to be able to edit the content of pages but not be able to add posts. There may be a custom post type that you have added that you do not want some users to have access to. It is fairly simple to add a snippet of code to your theme’s functions.php file that will check a user’s role or capabilities and remove menu items accordingly.

Remove Admin Menu based on User Role.

  • First you need to decide which user role you want to have access to the particular menu. In this example the WordPress Download Manager plugin has been installed and only Administrators should have access to the menu to add new downloads.
  • Secondly you need to find the link for the menu to add it to the remove part. You can find the menu link for any of the menu items in the left menu bar by hovering the mouse over them and checking the link in the browser. You are looking for the information after the ‘edit.php?’ part of the url. E.g. if you want to hide the Pages menu the full url will be similar to: ‘mywebsite.com/wp-admin/edit.php?post_type=page’ the part you need to know is ‘post_type=page’.

Once you have the above information you can create an action hook to amend the menu based on the user role. Take a look at the WordPress Codex to find other ways to check for roles or capabilites. This page also has useful information about Roles and Capabilities if you are building a plugin or theme.

The Code

https://gist.github.com/nickihastings/794eae2ae060a2e38a0f5811defd7fbc

First create the action hook and assign it a custom function. (Line 3)

Then write the function. Call the global variable for the user role. The If statement is checking whether the user is NOT an administrator, the exclamation mark is indicating the negative: Not. If the user is an admin the menu should display, for all other roles, (i.e. if the user is not an admin), the menu item should be removed. The menu that is being removed on Line 9 is the Download Manager custom post type menu. You can replace this with the menu you want to remove, e.g. post_type=page to remove the pages menu.

Once the above code is placed in the theme’s functions.php file, the menu will disappear from the Dashboard of all users who aren’t an administrator.

7 Comments

  • Diego

    Hi,
    it seems that it does not works… WordPress 5.6.1
    There is something wrong in my lil mod?
    Thank you
    Diego

    function nh_remove_menu_pages() {
    global $user_ID;
    //if the user is NOT an administrator remove the menu for downloads
    if ( !current_user_can( ‘administrator’ ) ) { //change role or capability here
    remove_menu_page( ‘admin.php?page=elementor’ ); //change menu item here
    remove_menu_page( ‘edit.php?post_type=elementor_library’ ); //change menu item here
    remove_menu_page( ‘tools.php’ ); //change menu item here
    remove_menu_page( ‘options-general.php’ ); //change menu item here
    }
    }

    • Nicki

      It is still working ok on the site I have it on with 5.6.1. It’s possible that there is some plugin or theme that is interfering with your code. You could try using a capability for the if statement, instead of the role ‘administrator’, this sometimes works better. For example:
      if ( ! current_user_can( 'manage_options' ) ) {
      The above is checking if the user has a capability of ‘manage_options’ which is an administrator capability.

  • Lynne

    I’m not having any luck. I’ve added the following in my snippits plugin – do you see an syntax errors? I’m trying to hide a admin menu item from my store vendors.

    Thanks,
    Lynne

    /* check if user is Administrator - if not, do not show Private Content Menu */

    add_action( 'admin_init', 'nh_remove_menu_pages' );

    function nh_remove_menu_pages() {
    global $user_ID;
    //if the user is NOT an administrator remove the menu for private content
    if ( !current_user_can( 'administrator' ) ) { //change role or capability here
    remove_menu_page( 'admin.php?page=pc_user_manage' ); //change menu item here
    }
    }

    • Nicki

      You could try changing your menu item line slightly, it is hard to find the correct menu slug. Try changing this line:

      remove_menu_page( ‘admin.php?page=pc_user_manage’ ); //change menu item here

      To this:

      remove_menu_page( 'pc_user_manage' ); //change menu item here

      and see if it works.

  • Lynne

    Nicki,

    Thanks. That worked great. I appreciate your help.

    Now I have another menu item that when I hover it shows “javascript:void(0)” and the subitems under that have the admin.php&page=…(screenshot: https://imgur.com/a/8uXS89l)

    I’ve tried the following – and have tried without the “admin.php&page=” as well. If you have any ideas, I’d appreciate it.
    ~Lynne

    add_action( ‘admin_init’, ‘nh_remove_menu_pages’ );

    function nh_remove_menu_pages() {
    global $user_ID;
    //if the user is NOT an administrator remove the menu for private content
    if ( !current_user_can( ‘administrator’ ) ) { //change role or capability here
    remove_menu_page( ‘pc_user_manage’ ); //change menu item here
    }
    if ( !current_user_can( ‘administrator’ ) ) { //change role or capability here
    remove_menu_page( ‘admin.php&page=pcfm_files_hub’ ); //change menu item here
    }

    }

    • Nicki

      If using just ‘pcfm_files_hub’ doesn’t work, then you’ll need to find out what the actual slug of the parent item is (that’s the one that says javascript:void). You can do that by running the piece of debug code below. Paste it into a new snippet and when it’s activated it will print to the screen a list of arrays that make up your admin menu. You may need to copy that text to a plain text file to read it better. You need to find the array that holds that menu, and then the value at [2] will be the slug you need. For example the Settings menu would look like this in array form:
      [80] => Array
      (
      [0] => Settings
      [1] => manage_options
      [2] => options-general.php
      [3] =>
      [4] => menu-top menu-icon-settings menu-top-last
      [5] => menu-settings
      [6] => dashicons-admin-settings
      )
      So the slug you need is at [2] which is ‘options-general.php’.

      The debug code can be found in this gist: https://gist.github.com/nickihastings/fcd472a84d0700c3cb74bb4dc15d5b41

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.