1

I am working on creating an administration menu within WordPress for a plugin. I have successfully registered the menu using add_menu_page() and it shows in the sidebar as expected. I have also added a couple of sections within the menu using add_submenu_page().

It appears that WordPress (running 3.05) is not correctly indicating which menu item should be activated as "current".

When I add a test page to the main Dashboard menu this is what I see:

http://dl.dropbox.com/u/3019972/wp-screen1.png

As you can see the current page is visually correct within the menu and is given a class of "current". However on my own menu it looks like this:

http://dl.dropbox.com/u/3019972/wp-screen2.png

In this example I would expect "Dashboard" to be highlighted.

Is this simply a WordPress bug? or do I need to provide special code to make this work within my own menu?

Updated with code examples

The code I am using for adding and creating menus looks like this:

add_action('admin_menu','my_admin_menu');

function my_admin_menu() {

// this test works as expected
// add_submenu_page( 'index.php', 'test', 'test', 'read', 'admin.php?page=plugin-dashboard','my_pluggin_dashboard' );

// my main menu menu    
add_menu_page(__('My Plugin', 'myplugin'),__('My Plugin', 'myplugin'), 'edit_posts','admin.php?page=plugin-dashboard','my_plugin_dashboard','icon');

// dashboard submenu - this fails to highlight with current
add_submenu_page('admin.php?page=plugin-dashboard', __('Dashboard','myplugin'), __('Dashboard','myplugin'), 'edit_posts', 'admin.php?page=plugin-dashboard', 'my_plugin_dashboard' );

// settings submenu - this fails to highlight with current
add_submenu_page('admin.php?page=plugin-dashboard', __('Settings','myplugin'), __('Settings','myplugin'), 'manage_options', 'admin.php?page=my-plugin-settings', 'my_plugin_settings' );

}

I have commented the code that works and the code that does not behave as I expect. Thanks for your comments.

4
  • Is your dashboard the current page? (or is that a duplicate link to the WP dashboard) If you're duplicating the WordPress dashboard link, there's no way the menu will know to apply a current class, it's just not that smart. It would help if you posted the code you're using to to add the menu items.
    – t31os
    Feb 19, 2011 at 15:03
  • Please post your code. If you simply add a submenu to your self-created main menu, highlighting should work out of the box. If you do more tricky stuff, see Mike's post or my response in Question [7029].
    – wyrfel
    Feb 19, 2011 at 15:24
  • @t31os I am adding my own dashboard menu and not moving the WordPress dashboard. I have added my code. @wyrfel I was expecting it to work out the box as well. I wonder if perhaps something is conflicting with my menu.
    – Scott
    Feb 19, 2011 at 18:02
  • Get rid of the admin.php?page= part.. (basically what wyrfel posted, mark his answer correct).
    – t31os
    Feb 20, 2011 at 1:24

1 Answer 1

3

Try this:

add_menu_page(__('My Plugin', 'myplugin'),__('My Plugin', 'myplugin'), 'edit_posts','my-plugin-dashboard','my_plugin_dashboard','icon');

// dashboard submenu - this fails to highlight with current
add_submenu_page('my-plugin-dashboard', __('Dashboard','myplugin'), __('Dashboard','myplugin'), 'edit_posts', 'my-plugin-dashboard', 'my_plugin_dashboard' );

// settings submenu - this fails to highlight with current
add_submenu_page('my-plugin-dashboard', __('Settings','myplugin'), __('Settings','myplugin'), 'manage_options', 'my-plugin-settings', 'my_plugin_settings' );

Essentially: don't use full page links with admin.php?... as the page slug. Especially if you are passing callbacks, just use a slug, not a page address.

1
  • 1
    I have the same issue as OP, but in my case the submenu links to edit-tags.php, so I can't use a proper slug. Any suggestions?
    – Dinei
    May 14, 2021 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.