Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - Remove submenu from custom post type

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' );
like image 515
Illes Peter Avatar asked Sep 30 '11 12:09

Illes Peter


People also ask

How do I get rid of submenu in WordPress?

You can figure it out by actually printing the global $submenu array to your debug. log file, and identifying the submenu you want to remove. global $submenu ; error_log (print_r( $submenu , true)); //this will print out all the menus/submenus currently available in the admin.


2 Answers

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.

like image 189
benz001 Avatar answered Oct 26 '22 17:10

benz001


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.

like image 38
CookiesForDevo Avatar answered Oct 26 '22 18:10

CookiesForDevo