0

*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-category&post_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 Q&A, by setting the show_in_menu parameter to false while registering the fp-teaser post type.

2
  • "the TDLRM item (previously created)" - what's your code for that?
    – Sally CJ
    Dec 8, 2021 at 17:15
  • 1
    Hi Sally CJ ) It's in another plugin: function tdlrm__menu_edit_main(){ add_menu_page( 'TDLRM', 'TDLRM', 'administrator', 'TDLRM', 'tdlrm__settings_page', 'dashicons-cart', 5 ) ;} add_action('admin_menu', 'tdlrm__menu_edit_main',10);
    – Artem
    Dec 9, 2021 at 1:37

1 Answer 1

1

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

4
  • 1
    Brilliant, works like a charm! Oh my, is it possible to know Wordpress like this )) 👍
    – Artem
    Dec 13, 2021 at 2:48
  • 1
    Side note to anyone interested. Note 2 from Sally CJ's answer works with the IF under the comment #2 in the wpse_398962_parent_file() function (one highlights the link, the other expands the parent menu item), so in this case I've made two separate filters, one for users and the other for categories.
    – Artem
    Dec 13, 2021 at 3:00
  • 1
    Also, on the 'however' part. The show_in_menu argument combined with the add_action priority works great. However, if we don't want to change the priority (say, the add_action is in another plugin), we can remove the new first sumbenu item with remove_submenu_page() and add the one we need with add_submenu_page() right away, setting the priority on that. The show_in_menu argument will highlight the new link and expand its parent menu.
    – Artem
    Dec 13, 2021 at 3:11
  • Thanks for the useful comments :)
    – Sally CJ
    Dec 13, 2021 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.