WordPress custom taxonomy link not highlighted under custom admin menu

I have registered a custom taxonomy for my custom post type:

$labels = array(
    ...labels here...
);

$args = array(
    'label'             => __('Categories', $this->text_domain),
    'labels'            => $labels,
    'public'            => true,
    'show_ui'           => true,
    'show_in_nav_menus' => true,
    'show_admin_column' => true,
    'hierarchical'      => true,
    'query_var'         => true,
    'rewrite'           => array('slug' => 'virtual-product-category'),
);

register_taxonomy('virtual_product_cat', array('virtual_product'), $args);

Works as expected – I can pick a custom category for my custom post.

Read More

Then I added it to a custom menu:

add_submenu_page(
    'virtual',
    __('Virtual Product Categories', $this->text_domain),
    __('Categories', $this->text_domain),
    'edit_products',
    'edit-tags.php?post_type=virtual_product&taxonomy=virtual_product_cat'
);

It shows up:

Virtual Products

When I click on it (the “Categories” link), taxonomy editing page loads fine, however, parent menu is displayed collapsed and the child (“Categories”) is not highlighted:

Virtual Product Categories

Custom post type (linke “Virtual Products”), on the other hand, works as expected (see the first picture).

I could do some hacks / workarounds, play with JS/CSS to make it highlighted but I think I am missing something here..

So, how do I make custom taxonomy menu link under custom menu work properly?

Thanks!

Related posts

Leave a Reply

4 comments

  1. Ok, for all folks who run into the same issue..

    What you are doing is:

    1. Creating a custom menu using add_menu_page
    2. Creating a custom post type / taxonomy
    3. Trying to push your custom taxonomy edit link under your new menu

    Do this instead:

    1. Do not create any menu manually (!)
    2. When adding custom post type, do not set 'show_in_menu' argument – this will create a menu for you. You can control where it appears by setting a 'menu_position' under the same args array.
    3. Use this newly created menu for all other custom pages that you want to add. When calling add_submenu_page, use 'edit.php?post_type=%%post_type_name%%' as a parent slug (the first param).

    Simply – don’t do the other way around 🙂

    Thanks to Obmerk Kronen and stink who tried to help. Appreciate this.

  2. I was looking for the same thing, but I did not want the to use the Custom Post Type as the main menu.

    I eventually found this link on the WP.org support forum http://wordpress.org/support/topic/moving-taxonomy-ui-to-another-main-menu

    Code outlined below.

    function recipe_tax_menu_correction($parent_file) {
        global $current_screen;
        $taxonomy = $current_screen->taxonomy;
        if ($taxonomy == 'ingredient' || $taxonomy == 'cuisine' || $taxonomy == 'course' || $taxonomy == 'skill_level')
            $parent_file = 'recipe_box_options';
        return $parent_file;
    }
    add_action('parent_file', 'recipe_tax_menu_correction');
    
  3. You could use this as a template or just use this taxonomy generator like I did.

    // Register Custom Taxonomy
    function Stack_Overflow()  {
    
    $labels = array(
        'name'                       => _x( 'Question', 'Taxonomy General Name', 'stack_overflow' ),
        'singular_name'              => _x( 'Question', 'Taxonomy Singular Name', 'stack_overflow' ),
        'menu_name'                  => __( 'Stack Overflow', 'stack_overflow' ),
        'all_items'                  => __( 'All Questions', 'stack_overflow' ),
        'parent_item'                => __( 'Language', 'stack_overflow' ),
        'parent_item_colon'          => __( 'Framework', 'stack_overflow' ),
        'new_item_name'              => __( 'New Question', 'stack_overflow' ),
        'add_new_item'               => __( 'Ask Question', 'stack_overflow' ),
        'edit_item'                  => __( 'Edit Question', 'stack_overflow' ),
        'update_item'                => __( 'Edit Question', 'stack_overflow' ),
        'separate_items_with_commas' => __( '', 'stack_overflow' ),
        'search_items'               => __( 'Search Questions', 'stack_overflow' ),
        'add_or_remove_items'        => __( 'Add or Remove Questions', 'stack_overflow' ),
        'choose_from_most_used'      => __( 'Choose from the most popular languages', 'stack_overflow' ),
    );
    $rewrite = array(
        'slug'                       => 'questions',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'query_var'                  => 'question',
        'rewrite'                    => $rewrite,
        'update_count_callback'      => 'display_edit_count',
    );
    register_taxonomy( 'stakoverflow', 'page', $args );
    
    }
    
    // Hook into the 'init' action
    add_action( 'init', 'Stack Overflow', 0 );
    
  4. @sPaul what you referenced in your answer works for custom post types but not for taxonomies. There isn’t a 'menu_position' arg for custom taxonomies. What you had done in your question will work fine to create the link under your menu:

    add_submenu_page(
        'virtual',
        __('Virtual Product Categories', $this->text_domain),
        __('Categories', $this->text_domain),
        'edit_products',
        'edit-tags.php?post_type=virtual_product&taxonomy=virtual_product_cat'
    );
    

    But to get it to show the correct styles you’ll need to add some JS to add / remove classes. Here’s how (modified from https://wordpress.stackexchange.com/a/225228/80088):

    add_action( 'admin_head-edit-tags.php', 'modify_menu_highlight_wpse_43839' );
    
    function modify_menu_highlight_wpse_43839()
    {
        if( 'virtual_product_cat' == $_GET['taxonomy'] )
        {       
            ?>
            <script type="text/javascript">
                jQuery(document).ready( function($) 
                {
                    $("#menu-posts, #menu-posts a")
                        .removeClass('wp-has-current-submenu')
                        .removeClass('wp-menu-open')
                        .addClass('wp-not-current-submenu'); 
                    $("#toplevel_page_ virtual, #toplevel_page_ virtual > a")
                        .addClass('wp-has-current-submenu');
                });     
            </script>
            <?php
        }
    }