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:
-
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
) tousers.php
. -
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“.