WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 6 months ago

#40649 new defect (bug)

parent_file filter seems to be overwritten by get_admin_page_parent call

Reported by: jontis Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.4
Component: Menus Keywords:
Focuses: administration Cc:

Description (last modified by welcher)

In wp-admin/menu-header.php the filter parent_file seems to have little to no impact most of the time.
This is because the global $parent_file variable often gets immediately overwritten by the get_admin_page_parent function call.

<?php
/**
 * Filters the parent file of an admin menu sub-menu item.
 *
 * Allows plugins to move sub-menu items around.
 *
 * @since MU
 *
 * @param string $parent_file The parent file.
 */
$parent_file = apply_filters( 'parent_file', $parent_file );

/**
 * Filters the file of an admin menu sub-menu item.
 *
 * @since 4.4.0
 *
 * @param string $submenu_file The submenu file.
 * @param string $parent_file  The submenu item's parent file.
 */
$submenu_file = apply_filters( 'submenu_file', $submenu_file, $parent_file );

get_admin_page_parent();

I am not completely sure what the purpose of the get_admin_page_parent is, but maybe the filter should be called after or inside that function?

To try to give an example. If I have the "Broken Link Checker" plugin installed and try to move it's settings page to the Tools menu I would want use this code:

<?php
add_filter('parent_file', 'move_to_tools');
function move_to_tools($parent_file){
  global $hook_suffix;

  if($hook_suffix === 'settings_page_link-checker-settings'){
    $parent_file = 'tools.php';
  }

  return $parent_file;
};

However, because of the above mentioned issue this code does nothing.
I am aware completely moving a settings page requires more work than this, this is purely an example.

Change History (4)

#1 @jontis
7 months ago

  • Component changed from General to Plugins

#2 @jontis
7 months ago

  • Component changed from Plugins to Menus

#3 @welcher
6 months ago

  • Description modified (diff)

#4 @welcher
6 months ago

  • Description modified (diff)

Thanks for the report @jontis!

Moving filters around can have unexpected consequences for plugins using them. That said, did you want to try and get a patch in place?

Note: See TracTickets for help on using tickets.