7

Using the remove_menu_page() function works for removing the default admin menu items by their slug like so:

add_action( 'admin_menu', 'hide_menu' );

function hide_menu() {
    remove_menu_page( 'index.php' ); // Dashboard
    remove_menu_page( 'tools.php' ); // Tools
}

When a plugin created their own menu in the Dashboard, the URL structure looks like the following:

http://example.com/wp-admin/admin.php?page=plugin-slug

However when trying to remove the custom plugin menu item like so:

remove_menu_page( 'admin.php?page=plugin-slug' );

Nothing changes. Looking at a similar questions here and here, it seems that my function isn't called in time once the custom plugin settings load? Yet, when I try to increase the priority to a higher number, that still doesn't work:

add_action( 'admin_menu', 'hide_menu', 9001, 1 );

Is there a work around? Am I doing this correctly?

1

3 Answers 3

15

Place this below temporary code in your functions.php or any where that can be executed.

add_action( 'admin_init', 'the_dramatist_debug_admin_menu' );

function the_dramatist_debug_admin_menu() {

    echo '<pre>' . print_r( $GLOBALS[ 'menu' ], TRUE) . '</pre>';
}

Then search for the plugin-slug. In which array you find it copy the [2] value and put it in remove_menu_page('the [2] value') and hook it to admin_init like below-

add_action('admin_init', '');
function the_dramatist_remove_menu(){
    remove_menu_page( 'the [2] value' );
});

And it will be working. And after it is working remove the temporary code block.

On the other hand, you can inspect the plugin code which menu page you wanna remove and in their add_menu_page() function take the fourth parameter of the add_menu_page() function and put it inside remove_menu_page('fourth parameter'). It will work as well. The code will look like below-

add_action('admin_init', '');
function the_dramatist_remove_menu(){
    remove_menu_page( 'fourth parameter of add_menu_page()' );
});
4
  • 1
    Thanks for the answer, looking at your answer, I've actually figured out that if I hook to the admin_init tag, I'm able to hide the custom setting menus. See my answer. Oct 18, 2016 at 15:01
  • +1 for the print_r Globals snippit
    – DGRFDSGN
    Nov 19, 2018 at 11:20
  • I was getting php warnings with admin_init. To fix that, I've used the admin_menu hook Aug 25, 2021 at 18:38
  • My two cents for the_dramatist_debug_admin_menu : as admin menu is still there, you need to move print_r results on the right to see everything : echo '<pre style="margin-left: 180px;">'.....
    – pirela
    Feb 27 at 11:21
8

Thanks to the answer that the_dramatist posted, it was a matter of just hooking to the admin_init tag. The slugs for those plugin pages can be retrieved by the debug script that the_dramatist provided, or you can simply look at that query value after admin.php?page=plugin-slug:

add_action( 'admin_menu', 'wpse_243070_hide_menu' );

function wpse_243070_hide_menu() {
    remove_menu_page( 'index.php' ); // Dashboard
    remove_menu_page( 'tools.php' ); // Tools
    remove_menu_page( 'plugin-slug' ); // Some plugin
    remove_menu_page( 'another_slug' ); // Another plugin
}
4
  • That is right, simple use remove_menu_page( 'menu_slug'). it does work well. May 9, 2018 at 19:16
  • 1
    Have you checked your logs? By doing this, sometimes you will get a PHP warning stating that there is an Invalid argument supplied for foreach() as documented here: codex.wordpress.org/Function_Reference/remove_menu_page under Usage. Dec 19, 2018 at 15:40
  • 1
    To fix the php warning issue, you should use the admin_menu hook instead of the admin_init Aug 25, 2021 at 18:36
  • 1
    @PabloSGPacheco Thanks for fix, I updated it in my answer. Aug 26, 2021 at 14:28
1

For me simple unsetting of menu elements proved to be the easiest and most versatile approach, this way you can for instance remove all menu items but leave only some desired like so:

add_filter('admin_menu', 'wpse_243070_hide_menu_items', 100);

function wpse_243070_hide_menu_items() {
  foreach ($GLOBALS['menu'] as $mkey => $mval) {
    if (!in_array($mval[2], [
                  'separator1',
                  'separator2',
                  'profile.php',
                  'edit.php?post_type=my_custom_post'
        ])) {
      unset($GLOBALS['menu'][$mkey]);
    }
  }
}
1
  • Any reasons for the downvote? It's the only way I can think of for removing all menu items but some chosen. It's also the only way that I know that works for removing some menu items created by plugins like ACF since the above approach didn't work.
    – Picard
    Jan 14, 2019 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.