Make WordPress Core

Opened 2 years ago

Last modified 22 months ago

#56480 new defect (bug)

_add_post_type_submenus doesn't use WP_Post_Type::$menu_position

Reported by: rahmohn's profile Rahmohn Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords:
Focuses: administration Cc:

Description

Although it's possible to add a post type as a submenu when registering by setting the property show_in_menu, it's not possible to set the position.

This is happening because in the function _add_post_type_submenus (wp-includes/post.php) the function add_submenu_page is called without passing the parameter $position.

<?php
function _add_post_type_submenus() {
        foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
                $ptype_obj = get_post_type_object( $ptype );
                // Sub-menus only.
                if ( ! $ptype_obj->show_in_menu || true === $ptype_obj->show_in_menu ) {
                        continue;
                }
                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" );
        }
}

Steps to reproduce the issue:

  1. Add the code snippet in the functions.php of the theme (notice the 'menu_position')
<?php
register_post_type(
        'foo',
        array(
                'labels'        => array(
                        'name' => 'Foo',
                ),
                'show_in_menu'  => 'edit.php?post_type=page',
                'menu_position' => 20,
                'public'        => true,
        )
);

register_post_type(
        'bar',
        array(
                'labels'        => array(
                        'name' => 'Bar',
                ),
                'show_in_menu'  => 'edit.php?post_type=page',
                'menu_position' => 10,
                'public'        => true,
        )
);
  1. Visit the Dashboard and check that under the menu Pages, "Foo" appears before "Bar"

Expected behaviour: "Foo" appears after "Bar"

Attachments (1)

Screenshot from 2022-08-31 14-18-22.png (6.9 KB) - added by Rahmohn 2 years ago.

Download all attachments as: .zip

Change History (3)

#1 @desrosj
2 years ago

  • Version trunk deleted

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


22 months ago

Note: See TracTickets for help on using tickets.