[wp-trac] [WordPress Trac] #19085: Removing First Submenu Page in Admin Menu breaks URL for Menu Page

WordPress Trac noreply at wordpress.org
Wed Jun 18 15:07:35 UTC 2025


#19085: Removing First Submenu Page in Admin Menu breaks URL for Menu Page
--------------------------------------------------+---------------------
 Reporter:  mikeschinkel                          |       Owner:  (none)
     Type:  defect (bug)                          |      Status:  new
 Priority:  normal                                |   Milestone:
Component:  Administration                        |     Version:  3.1
 Severity:  normal                                |  Resolution:
 Keywords:  has-patch needs-test-info needs-docs  |     Focuses:
--------------------------------------------------+---------------------
Changes (by SirLouen):

 * keywords:  needs-testing has-patch => has-patch needs-test-info needs-
               docs


Comment:

 == Test Report
 === Description
 ✅ This report validates that the indicated patch works as expected.

 Patch tested:
 https://core.trac.wordpress.org/attachment/ticket/19085/submenuasparent.19085.diff

 === Environment
 - WordPress: 6.9-alpha-60093-src
 - PHP: 7.4.33
 - Server: nginx/1.27.5
 - Database: mysqli (Server: 8.4.5 / Client: mysqlnd 7.4.33)
 - Browser: Chrome 137.0.0.0
 - OS: Windows 10/11
 - Theme: Twenty Twenty-Five 1.2
 - MU Plugins: None activated
 - Plugins:
   * Custom Plugin 1.0
   * Test Reports 1.2.0

 === Test Instructions
 - Keep adding new code to check everything. Add code in a plugin or
 `functions.php` or wherever it can be executed. Code samples
 [https://core.trac.wordpress.org/ticket/19085#comment:14 taken from here]
 and adapted to this testing report.

 1. Add `Common menus code`
 2. Check menus created
 3. ✅ Everything looks in place
 4. Add `Removing Submenu Main`
 5. Check that main menu has been removed
 6. 🐞 Both Main Page and Sub Page now point to subpage. It's impossible to
 go into Main page.

 === Expected Results

 - It should be able to point to Main page, from the Main page menu,
 despite of having removed the submenu.

 === Actual Results with PPatch
 - ✅ Issue resolved with patch.

 === Additional Notes
 1. With the proposed hook filter it is possible to solve this problem as
 shown in last supplemental artifact.

 2. But still, for the second filter some test info/example is required.
 Furthermore, that filter should be done in the classic way (add a
 variable, add some docs and pass the variable with the filter to
 `_wp_menu_output` instead of just passing the output form filter straight
 away)

 3. Hook Filter Docs are required

 cc @chrisguitarguy

 === Supplemental Artifacts

 === Common menus code

 {{{
 add_action('admin_menu', 'wpse206471_add_pages');
 function wpse206471_add_pages()
 {
     $hook = add_menu_page(
         __('WPSE206471 Main Page', 'wpse206471'),
         __('Main Page', 'wpse206471'),
         'manage_options',
         'wpse206471-main',
         'wpse206471_main_page'
     );
     add_submenu_page(
         'wpse206471-main',
         __('WPSE206471 Sub Page', 'wpse206471'),
         __('Sub Page', 'wpse206471'),
         'manage_options',
         'wpse206471-sub',
         'wpse206471_sub_page'
     );
 }
 }}}

 Result:
 [[Image(https://i.imgur.com/bHnaNjK.png)]]

 === Removing Submenu Main

 {{{
 add_action('admin_menu', 'wpse206471_add_pages');
 function wpse206471_add_pages()
 {
     $hook = add_menu_page(
         __('WPSE206471 Main Page', 'wpse206471'),
         __('Main Page', 'wpse206471'),
         'manage_options',
         'wpse206471-main',
         'wpse206471_main_page'
     );
     add_submenu_page(
         'wpse206471-main',
         __('WPSE206471 Sub Page', 'wpse206471'),
         __('Sub Page', 'wpse206471'),
         'manage_options',
         'wpse206471-sub',
         'wpse206471_sub_page'
     );
 +   remove_submenu_page('wpse206471-main', 'wpse206471-main');
 }
 }}}

 Result:
 [[Image(https://i.imgur.com/Fhp8hX9.png)]]

 === Patch + Filter

 {{{
 add_action('admin_menu', 'wpse206471_add_pages');
 function wpse206471_add_pages()
 {
     $hook = add_menu_page(
         __('WPSE206471 Main Page', 'wpse206471'),
         __('Main Page', 'wpse206471'),
         'manage_options',
         'wpse206471-main',
         'wpse206471_main_page'
     );
     add_submenu_page(
         'wpse206471-main',
         __('WPSE206471 Sub Page', 'wpse206471'),
         __('Sub Page', 'wpse206471'),
         'manage_options',
         'wpse206471-sub',
         'wpse206471_sub_page'
     );

     // remove the "main" submenue page
     remove_submenu_page('wpse206471-main', 'wpse206471-main');
     // tell `_wp_menu_output` not to use the submenu item as its link
 +   add_filter("submenu_as_parent_{$hook}", '__return_false');
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/19085#comment:15>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list