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.

Read More

I tried:

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

Related posts

Leave a Reply

4 comments

  1. 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.

  2. 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.

  3. It might be better to just use the 'show_ui' => false.

    function car_brand_init() {
        // new taxonomy named merk
        register_taxonomy(
            'merk',
            'lease_fop',
            array(
                'label' => __( 'Merken' ),
                'rewrite' => array( 'slug' => 'merken' ),
                'update_count_callback' => '_update_post_term_count',
                // use this to hide from menu
                'show_ui' => false,
                'capabilities' => array(
                    'assign_terms' => 'edit_guides',
                    'edit_terms' => 'publish_guides'
                )
            )
        );
    }
    add_action( 'init', 'car_brand_init' );
    
  4. To build on the answer from benz001:

    No need to do the var dump to get the slug right. This worked for me.

    remove_submenu_page('edit.php?post_type=POST_TYPE', 'edit-tags.php?taxonomy=TAXONOMY_SLUG&post_type=POST_TYPE')
    

    First argument, replace POST_TYPE with what you see in your URL field at the top of the browser when viewing archive in back end.

    Second Argument: Left click on the submenu you want to remove, copy the link, remove the last forward slash and everything to the left of it. Replace the & with &

    Second arg example below.

    1 Copy and paste submenu link

    http://yourwebsite.com/wp-admin/edit-tags.php?taxonomy=your_custom_taxonomy&post_type=your_custom_post
    

    2 Remove last forward slash and everything left of it.

    edit-tags.php?taxonomy=your_custom_taxonomy&post_type=your_custom_post
    

    3 Replace the & with the HTML entity &

    /edit-tags.php?taxonomy=your_custom_taxonomy&post_type=your_custom_post