Question

For the plugin I'm building I have added a submenu as a child to a custom main menu item in admin. The submenu page is an edit-tags.php page. This shows up as intended under the main menu item. When clicking on this sub menu item, the user is taken to the correct page, however, the main menu item collapses back down, hiding the currently open sub menu item.

My initial feeling is that the parent slug attribute is wrong, but other submenus I have added use the same parent slug attribute and they work correctly. I'm clearly missing some option in the add_submenu_page() function, but I cannot figure out what.

Here is how I am registering the sub menu:

$this->plugin_screen_dashboard = add_submenu_page(
    $this->plugin_slug, // Parent slug
    __( 'Subscriber Lists', $this->plugin_slug ), // Page title
    __( 'Subscriber Lists', $this->plugin_slug ), // Menu title
    'manage_options', // Capability
    'edit-tags.php?taxonomy=subscriber_list&post_type=subscriber' // Menu slug
);

--Edit--

Here is how I register the main menu item:

$this->plugin_screen_hook_suffix = add_menu_page(
    __( 'My Plugin', $this->plugin_slug ), // Page title
    __( 'My Plugin', $this->plugin_slug ), // Menu title
    'manage_options', // Capability
    $this->plugin_slug, // Menu slug
    array( $this, 'display_plugin_admin_page' ), // Function
    'dashicons-plus-alt' // Icon url
);
Était-ce utile?

La solution

I've managed to solve this using Javascript to 'open' the menu item when on the edit tags page in the plugin.

Relevant plugin PHP file

$screen = get_current_screen();
// Check we're only on the edit-tags page in the plugin
if ('edit-tags' === $screen->base && 'subscriber' === $screen->post_type) {
    wp_enqueue_script( $this->plugin_slug . '-subscriber-edit-tags-script', plugins_url('assets/js/subscriber-edit-tags.js', __FILE__ ), array('jquery') );
}

subscriber-edit-tags.js (using jQuery)

(function ( $ ) {
    /**
     * File is called only when on edit tags under subscriber post type
     */

    $('.toplevel_page_my_plugin')
        .removeClass('wp-not-current-submenu')
        .addClass('wp-has-current-submenu wp-menu-open')
        .find('li').has('a[href*="edit-tags.php"]')
        .addClass('current');

}(jQuery));

Autres conseils

I had a similar issue and was able to resolve the improper menu behavior by using the parent_file filter. $parent_file is set in the /wp-admin/edit-tags.php file, but it needs to be altered based on your use case.

add_filter('parent_file', 'filter_subscriber_menu');

function filter_subscriber_menu($file) {
    $screen = get_current_screen();
    if ('edit-tags' === $screen->base && 'subscriber' === $screen->post_type) {           
    // in my case I drilled down to if($screen->id...); I used what you posted in your if clause above     
    $file = {$this->plugin_slug}; //probably need to set this as a string; for the parent slug represented by $this->plugin_slug ;  
    }
return $file;

}

For anyone that's looking at this now (March 2019), unfortunately this problem still exists in 5.1...!

So the HTML elements in the WP menu have changed, so now this jQuery script works:

jQuery(document).ready( function($) {
    $(".menu-icon-sdesk-kb#menu-posts-sdesk-kb")
    .removeClass("wp-not-current-submenu")
    .addClass("wp-has-current-submenu wp-menu-open")

    $("#menu-posts-sdesk-kb a.wp-not-current-submenu")
    .removeClass("wp-not-current-submenu")
    .addClass("wp-has-current-submenu wp-menu-open")
});
  • sdesk-kb = my CPT taxonomy name
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top