Wordpress - Remove submenu from custom post type

10,912

Solution 1

This is a bit dirty, but it works:

add_action('admin_menu', 'remove_niggly_bits');
function remove_niggly_bits() {
    global $submenu;
    unset($submenu['edit.php?post_type=portfolio'][11]);
}

I'm not sure exactly which key number you'll want to remove. Best way to find that is to do:

add_action('admin_menu', 'remove_niggly_bits');
function remove_niggly_bits() {
    global $submenu;
    //unset($submenu['edit.php?post_type=portfolio'][11]);
    print_r($submenu); exit;
}

Everything will break when you load the admin area until you remove that line, but it'll show you the structure and which key you need.

Solution 2

You can use remove_submenu_page() - the trick however is to get the slug exactly right, to do this the easiest way is to dump the global $submenu and check for the menu_slug and submenu_slug.

global $submenu;
var_dump($submenu);

This will give you the array of menus, the top level key is the menu_slug and then the correct submenu_slug can be found in the element 2 of the nested arrays.

So if I had a custom post type called "my_events" and I wanted to remove the tags menu from it, my original menu structure would look like this

...
'edit.php?post_type=my_events' => 
    array
      5 => 
        array
          0 => string 'All Events' (length=10)
          1 => string 'edit_posts' (length=10)
          2 => string 'edit.php?post_type=my_events' (length=28)
      10 => 
        array
          0 => string 'Add New' (length=7)
          1 => string 'edit_posts' (length=10)
          2 => string 'post-new.php?post_type=my_events' (length=32)
      15 => 
        array
          0 => string 'Tags' (length=4)
          1 => string 'manage_categories' (length=17)
          2 => string 'edit-tags.php?taxonomy=post_tag&post_type=my_events' (length=55)
...

From this you can see that the menu_slug is 'edit.php?post_type=my_events' and the submenu slug for the tags menu is 'edit-tags.php?taxonomy=post_tag&post_type=my_events'.

So the remove function call would be:

remove_submenu_page('edit.php?post_type=my_events', 'edit-tags.php?taxonomy=post_tag&post_type=my_events');

Note that the submenu slug is html encoded so the ampersand is now & - this is probably that thing that has made it hard for people to work out from first principles what the slug name should be.

Share:
10,912

Related videos on Youtube

Illes Peter
Author by

Illes Peter

I am a 26 year old freelance graphic designer and web developer from Timisoara. C4studio is my umbrella brand under which I am creating visual identity, print and web media.

Updated on June 04, 2022

Comments

  • Illes Peter
    Illes Peter almost 2 years

    I created a custom post type named portfolio with tag taxonomy support.

    Since WP does not make a difference between post tags and custom post type tags, I created a menu item Taxonomy under which I want to put categories and post tags. I managed to create the the menu and submenus, and also remove category and post tags from the Post menu, but i didn't manage to remove Post Tags from the custom post type menu.

    I tried:

    remove_submenu_page( 'edit.php?post_type=portfolio', 'edit-tags.php?taxonomy=post_tag&post_type=portfolio' );
    
  • Illes Peter
    Illes Peter over 12 years
    It is a bit dirty. I hoped for it to work with remove_submenu_page(), but since it is the only (working) answer you get the accepted answer :-)
  • Sparky
    Sparky over 10 years
    +1, it works. @IllesPeter, this should be the accepted answer.
  • Rafff
    Rafff about 9 years
    But this also disables the meta box. If you want to only hide the taxonomy from the menu, @benz001 solution is the best.
  • Philip Jones
    Philip Jones about 9 years
    +1 confirmed it works. As reported elsewhere, this function is very sensitive to getting both arguments spot on. It's worth persisting with different tweaks to get it right.
  • user2655393
    user2655393 almost 8 years
    +1 SUPER HELPFUL! Thanks @benz001 Should be the correct answer.
  • José Neto
    José Neto over 2 years
    The encoded character & was an important tip. Thanks!