WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 20 months ago

#22022 new defect (bug)

Can’t properly add pages of type edit.php?post_type=xxx as submenu items to arbitrary parent menus — at Version 3

Reported by: jjharr Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.0
Component: Administration Keywords: reporter-feedback
Focuses: Cc:

Description (last modified by SergeyBiryukov)

The following code illustrates the problem.

add_menu_page('My Pages', 'My Pages', 'edit_posts', 'parentslug', array(class, func));
add_submenu_page('parentslug', 'Settings', 'Settings', 'edit_posts', 'mysettings', array(class, func));
add_submenu_page('parentslug', 'Custom Post Type', 'Custom Post Type', 'edit_posts', 'edit.php?post_type=xxx'); 

When you click on the first submenu item, the menu stays open, displaying the submenu items as it should. When you click on the item for the custom post type, the parent menu is closed and submenu pages are not displayed.

The root of the problem is how $parent_page is handled for pages of type edit.php?post_type=xxx. In get_admin_page_parent(), $parent_file is always set to $submenu[$parent], which may cause the submenu slug to be different from $parent_file. But in _wp_menu_output, if the submenu slug and parent slug are not equal, the 'wp-has-current-submenu wp-menu-open' classes do not get added which means the menu gets displayed as being closed.

Since there are no actions called between get_admin_parent_page() and the output of the menu, the only workaround I can see is some ugly JS that fixes up the classes.

It seems like the least intrusive fix would be to just move the call to apply_filters("parent_file) in menu-header.php to after the call to get_admin_page_parent() so that $parent_file can truly be overridden. This also seems inline with the intent of the filter.

Change History (3)

comment:1 @jjharr2 years ago

  • Cc jjharr added

Providing a simpler explanation and a patch in hopes that something happens with this issue :

Simpler explanation : in menu-header.php, the 'parent_file' filter, which allows the user to set $parent_file, runs before the get_admin_page_parent() function. But the get_admin_page_parent() function can change $parent_file, overriding the filter value and rendering it ineffective. So the filter needs to run after that function.

Patch : All this does is swap the filter and function lines. The only way this would break existing code is if someone was relying on the internal behavior of get_admin_page_parent() to override the value they set in their filter function - which is not very likely.

=====================

--- wp-admin/menu-header.php
+++ wp-admin/menu-header.php
@@ -19,10 +19,11 @@ $self = preg_replace('|^.*/plugins/|i', '', $self);
 $self = preg_replace('|^.*/mu-plugins/|i', '', $self);

 global $menu, $submenu, $parent_file; //For when admin-header is included from within a function.
-$parent_file = apply_filters("parent_file", $parent_file); // For plugins to move submenu tabs around.

 get_admin_page_parent();

+$parent_file = apply_filters("parent_file", $parent_file); // For plugins to move submenu tabs around.
+
 /**
  * Display menu.
  *

=====================

Last edited 2 years ago by jjharr (previous) (diff)

comment:2 @SergeyBiryukov2 years ago

  • Component changed from Menus to Administration

comment:3 @SergeyBiryukov2 years ago

  • Description modified (diff)
Note: See TracTickets for help on using tickets.