Make WordPress Core

Opened 6 months ago

Last modified 6 months ago

#58062 new defect (bug)

Positioning of custom post type submenu

Reported by: mort1305's profile mort1305 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.1
Component: Posts, Post Types Keywords: needs-patch
Focuses: ui, administration Cc:

Description (last modified by sabernhardt)

Current Functionality (since 3.1.0): One can add a CPT as a submenu of another CPT by setting the show_in_menu argument of the register_post_type() function as edit.php?post_type=CUSTOM_CPT_SLUG. The submenu is then added into the administration menu by way of the _add_post_type_submenus() function. When this occurs, the submenu CPT's are displayed in the order the CPTs were registered.

Problem: The current coding prohibits the ordering of CPT submenu items per the programmer's desires in the event CPT registration is unable to be controlled or in the event other submenu pages are added via the add_submenu_page() method. This occurs because the call to add_submenu_page() by _add_post_type_submenus() only passes 5 arguments. (The sixth omitted argument is what handles menu positioning.)

Solution: The menu_position argument passed to the register_post_type() function already exists, and defines a CPT's positioning within a menu. The solution is to incorporate this argument into the core by modifying line 2079 of wp-includes/post.php as follows:

add_submenu_page(
   $ptype_obj->show_in_menu,
   $ptype_obj->labels->name,
   $ptype_obj->labels->all_items,
   $ptype_obj->cap->edit_posts,
   "edit.php?post_type=$ptype",
   isset($ptype_obj->menu_position) ? $ptype_obj->menu_position : NULL
);

Change History (3)

#1 @sabernhardt
6 months ago

  • Component changed from Menus to Posts, Post Types
  • Description modified (diff)

#2 @oglekler
6 months ago

  • Keywords needs-patch added
  • Version changed from 6.2 to 3.1

This is exactly as described. When a new post type is registered via [register_post_type](https://developer.wordpress.org/reference/functions/register_post_type/) function and show_in_menu is equal to parent menu slug such as 'edit.php?post_type=page' this custom post type will be placed into this parent submenu but 'menu_position' argument has no effect on this.

Modified code is provided by reporter is to _add_post_type_submenus() function.

It looks like a strait forward patch, and the only downside of this will be that custom post types used in submenus will suddenly be affected by menu order if it exists.

Related tickets: #56480, #52035, #19541, possibly one of them can be counted as an original and this one can be closed in favour of the early one.

Last edited 6 months ago by oglekler (previous) (diff)

This ticket was mentioned in Slack in #core by oglekler. View the logs.


6 months ago

Note: See TracTickets for help on using tickets.