Visiting a console submenu page does not expand its parent menu item

*I have moved a custom post taxonomy to another menu item

//created a post type that does not appear in admin menu, therefore its taxonomy does not, too
    $args = array(
            'label'                 = 'Item',
            'description'           = 'Store items',
            'labels'                = $labels,
            'supports'              = array( 'title', 'editor', 'thumbnail', 'comments', 'revisions' ),
            'taxonomies'            = array( 'store-category' ),
            'hierarchical'          = false,
            'public'                = true,
            'show_ui'               = true,
            'show_in_menu'          = false,
            'menu_position'         = 5,
            'menu_icon'             = 'dashicons-cart',
            'show_in_admin_bar'     = true,
            'show_in_nav_menus'     = true,
            'can_export'            = true,
            'has_archive'           = true,
            'exclude_from_search'   = false,   
            'publicly_queryable'    = true,
            'capability_type'       = 'post',
            'rewrite'               = array('slug' = 'product')
        );
    register_post_type( 'tdlrm_store_item', $args );

//registered a taxonomy
$args = array(
        'labels'                     = $labels,
        'hierarchical'               = true,
        'public'                     = true,
        'show_ui'                    = true,
        'show_admin_column'          = true,
        'show_in_nav_menus'          = true,
        'show_tagcloud'              = false,
        'rewrite'                    = array('slug' = 'group')
    );
    register_taxonomy( 'store-category', array( 'tdlrm_store_item' ), $args );     

//added a submenu page for the taxonomy into another menu item
   add_action('admin_menu', 'tdlrm_configure_admin_menu1',10);
   function tdlrm_configure_admin_menu1(){
      add_submenu_page('TDLRM', 'category_redirect', 'Categories', 'administrator',  'edit-tags.php?taxonomy=store-categorypost_type=tdlrm_store_item');
   }

When I visit the page, no menu items get expanded, I would like the TDLRM item (previously created) to expand.

*I have created a submenu page for users with a certain role. When I visit the page, it expands the Users menu item.

add_action('admin_menu', 'tdlrm_configure_admin_menu2',10);
function tdlrm_configure_admin_menu2(){
    add_submenu_page('TDLRM', 'store_users_redirect', 'Store users', 'administrator',  'users.php?role=tdlrm_store_user');
}

I would like the TDLRM item to expand instead.

There was also a similar task that I solved while writing this question, although the solution does not apply to the other two.

I have created a custom post type, its slug is 'fp-teaser'. It created an admin menu item with the following slug: 'edit.php?post_type=fp-teaser'.

I then moved the new menu item under another menu item like this:

add_action('admin_menu', 'tdlrmm__menu_edit', 20);
function tdlrmm__menu_edit(){
    // removing the page from top-level menu
    remove_menu_page( 'edit.php?post_type=fp-teaser');
    
    // creating the page under My-menu
    // (My-menu is a page I created earlier.)
    add_submenu_page('My-menu', 'Teasers', 'Teasers', 'edit_posts',  'edit.php?post_type=fp-teaser');
}

The problem was that when I visited /wp-admin/edit.php?post_type=fp-teaser, no menu item got expanded. What I wanted was the My-menu item to expand showing the Teasers sub-menu item.

This has been solved after finding this QA, by setting the show_in_menu parameter to false while registering the fp-teaser post type.

Topic add-submenu-page admin-menu Wordpress

Category Web


You can use the parent_file hook to ensure the TDLRM menu is expanded when viewing the "Store users" or "Categories" page.

add_filter( 'parent_file', 'wpse_398962_parent_file' );
function wpse_398962_parent_file( $parent_file ) {
    global $submenu_file, $typenow;

    // 1: This is for highlighting the TDLRM » "Store users" submenu.
    if ( 'users.php' === $parent_file && ! $submenu_file &&
        isset( $_GET['role'] ) && 'tdlrm_store_user' === $_GET['role']
    ) {
        $typenow = true; // this trick ensures the parent file/slug is TDLRM

        // This highlights the "Store users" submenu.
        // Make sure it matches the slug you passed to add_submenu_page().
        $submenu_file = 'users.php?role=tdlrm_store_user';

        return 'TDLRM';
    }

    // 2: This is for highlighting the TDLRM » Categories submenu.
    // Make sure the value matches the slug you passed to add_submenu_page().
    if ( 'edit-tags.php?taxonomy=store-category&post_type=tdlrm_store_item' === $submenu_file ) {
        return 'TDLRM';
    }

    return $parent_file;
}

Notes:

  1. There is a trick above which is used for expanding the TDLRM menu, but only when viewing the "Store users" page.

    And what the trick does is, it prevents get_admin_page_parent() from changing the parent file ($parent_file) to users.php.

  2. WordPress escapes special characters like ampersands (so & becomes &) in the menu/submenu slug, hence your "Categories" submenu needs to use a valid/properly-escaped slug in order for the submenu to be highlighted when viewing the TDLRM » Categories page.

    // Note below I used & instead of just &
    add_submenu_page('TDLRM', 'category_redirect', 'Categories', 'administrator',
        'edit-tags.php?taxonomy=store-category&post_type=tdlrm_store_item');
    

And actually, instead of having to use the tdlrmm__menu_edit() function, you could simply set the show_in_menu arg to My-menu (the menu slug), and then "Teasers" ( or whatever is the (plural) label of your post type ) would appear as a submenu under your "My-menu" menu.

However, be sure to read the note here which says, "this item will become the first submenu item, and replace the location of the top-level link. If this isn’t desired, the plugin that creates the menu page needs to set the add_action priority for admin_menu to 9 or lower".

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.