Current class on admin menu using add_submenu_page()

The question:

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.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment