[wp-trac] [WordPress Trac] #32422: add_menu_page() returns incorrect $hookname when using plugin filename as $menu_slug

WordPress Trac noreply at wordpress.org
Sun May 17 05:06:32 UTC 2015


#32422: add_menu_page() returns incorrect $hookname when using plugin filename as
$menu_slug
----------------------------+-----------------------------
 Reporter:  quinncom        |      Owner:
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  Plugins         |    Version:  4.2.2
 Severity:  normal          |   Keywords:
  Focuses:  administration  |
----------------------------+-----------------------------
 When using `add_menu_page()` and `add_submenu_page()` with filenames in
 the $menu_slug parameter, the resulting hookname does not match the
 expected `do_action('load-' …)` hookname.

 In the following examples the expected hookname from `do_action( 'load-' .
 $plugin_page );` (in ''wp-admin/admin.php'') is: '''load-
 myplugin/admin/resources.php'''. However, the hookname returned by
 `$hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);` (from
 `add_submenu_page()` in ''wp-admin/includes/plugin.php'') is
 '''myplugin_page_myplugin/admin/resources'''.

 The result is, this code does not work:

 {{{
 $hookname = add_submenu_page("myplugin/dashboard.php", 'Resources',
 'Resources', 'edit_pages', "myplugin/resources.php");
 add_action($hookname, 'add_resources_options' );
 function add_resources_options() {
     add_screen_option('per_page', array(
         'label' => 'Resources',
         'default' => 10,
         'option' => 'resources_per_page'
     ));
 }
 }}}

 However, the hooknames do match when using `add_menu_page()` and
 `add_submenu_page()` with a callback slugname. This code works (notice
 only the first line has been changed to use callback slugs instead of
 filename slugs):

 {{{
 $hookname = add_submenu_page('dashboard', 'Resources', 'Resources',
 'edit_pages', 'myresources', 'myresources');
 add_action($hookname, 'add_resources_options' );
 function add_resources_options() {
     add_screen_option('per_page', array(
         'label' => 'Resources',
         'default' => 10,
         'option' => 'resources_per_page'
     ));
 }
 }}}

 The solution is either:

  * Modify `function get_plugin_page_hookname() …` to return a hookname
 that is caught by `do_action( 'load-' . $plugin_page );`.
  * Or modify `do_action( 'load-' . $plugin_page );` to use the hookname
 currently returned by `get_plugin_page_hookname()`.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/32422>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list