I have a custom post type, Jobs, with 3 taxonomies under it in the Admin menu.

One of the taxonomies is Status, which is either Active or Closed. I want a menu item under Jobs for Active Jobs. I created it with this code

add_submenu_page(
    'edit.php?post_type=jobs',
    'Active Jobs',
    'Active Jobs',
    'manage_options',
    'edit.php?post_type=jobs&jobstatus=67'
);

This works perfectly, except that the Jobs menu item remains highlighted when the Active Jobs menu option is active. See screen shot

example of submenu display

I read in this article
Current class on admin menu using add_submenu_page()
to not include the parent slug as the first parameter. I don’t know how to get the submenu to appear in the correct nav section when I remove the filename slug.

At this point I am not using a callback function, I am simply executing the same URL as the default Jobs submenu option, except with query params that filter the posts displayed. If moving this into a callback function will resolve the issue, I can do that. But I don’t know what should go in the callback function. I want to display the standard custom post type edit page, just with a taxonomy filter in place.
thanks for assistance

4 s
4

Here is a solution I just came up with which doesn’t use jQuery:

There is a filter parent_file in wp-admin/menu-header.php which runs right before outputting the menu. The inline comment says:

For plugins to move submenu tabs around.

It is just a filter on the global variable $parent_file and I am not sure what it does but we will use this filter to alter the global variable $submenu_file instead, which set the highlighted submenu. So this will be the solution in your case:

add_filter('parent_file', 'wpse44270_parent_file');

function wpse44270_parent_file($parent_file){
    global $submenu_file;
    if (isset($_GET['jobstatus']) && $_GET['jobstatus'] == 67) $submenu_file="edit.php?post_type=jobs&jobstatus=67";

    return $parent_file;
}

You may adapt this with any url formatting. For example I use the format admin.php?page=my_plugin_slug&action=myaction for my plugins’ sub menus so I used this to highlight my sub menus:

add_filter('parent_file', 'wpse44270_1_parent_file');

function wpse44270_1_parent_file($parent_file){
    global $submenu_file;
    if (isset($_GET['page']) && isset($_GET['action'])) $submenu_file = $_GET['page'] . '&action=' . $_GET['action'];

    return $parent_file;
}

PS: I also tried the action admin_menu to set $submenu_file, and it did work in my case (custom plugin page/slug) but not for edit.php sub menus (your case). So I searched for another action/filter that runs later and it was the filter parent_file.

Leave a Reply

Your email address will not be published. Required fields are marked *