Manually highlight a Wordpress admin menu item

In the Pages menu of the Wordpress Admin page, I got this layout:

Pages

  • Edit (url: edit-pages.php)
  • Add new (url: page-new.php)
  • Special pages (url: edit-pages.php? Special-pages = true)

as you can see, I added a new item to the Special Pages submenu, which is pretty much a link to the edit page using a custom filter. Since Wordpress uses the file name to identify and highlight the submenu item, so when I click on the special pages, the "Change" submenu item is selected. Is there a way to get Wordpress to select the Special Pages menu item instead?

Greetings

+7
wordpress
source share
5 answers

Solution: use the $ submenu_file variable

$ submenu_file = "edit-pages.php? special-pages = true"

+3
source share

To clarify Ken Wu’s answer, change the global variables $submenu_file and $parent_file . For example, to highlight your page:

 global $submenu_file; $submenu_file = "edit-pages.php?special-pages=true"; 

If you need to change the selected top-level element, use $parent_file . For example, highlight the "Writing" settings page:

 global $parent_file; global $submenu_file; $parent_file = 'options-general.php'; $submenu_file = 'options-writing.php'; 
+5
source share

the best solution:

 add_filter('parent_file', 'my_plugin_select_submenu'); function my_plugin_select_submenu($file) { global $plugin_page; if ('__my-current-submenu-slug__' == $plugin_page) { $plugin_page = '__my-submenu-slug-to-select__'; } return $file; } 
+3
source share

Thanks to Ken Wu and Jonathan Brinley. Using your answers, I finally got the highlight of my admin menu to work properly. Since I was struggling to get it working, I would post the whole result here so that other people can find it easier:

The idea is to call the parent_file filter (undocumented, since many parts of Wordpress are not random). In my case, I added a custom menu instead of the default value created when creating a custom message type.

In my custom zip code, I call the add_meta_boxes action. In this case, I call my call to the parent_file filter:

 add_filter('parent_file', array(&$this, 'highlight_admin_menu')); 

_

Then my hightlight_admin_menu function looks like hightlight_admin_menu :

 function highlight_admin_menu($some_slug){ global $parent_file; $parent_file = 'post.php?post=149&action=edit'; return $parent_file; } 

_

This is my menu to highlight correctly. Try playing with your own code to find out where to add_filter('parent_file', ...) code. Find a bit of code executed only on this particular page load, but soon enough that you can still change the $parent_file variable.

Hope this helps!

+2
source share

To change the highlighted menu item for a submenu item, the correct filter is submenu_file .

 add_filter('submenu_file', 'menuBold'); static function menuBold($submenu_file) { if ( checkProperPage($_GET) ) { // The address of the link to be highlighted return 'post-new?post_type=foobar&foo=bar'; } // Don't change anything return $submenu_file; } 

The verification takes place in the file WP ~/wp-admin/menu-header.php on line 194 (Wordpress 4.5.3):

 if ( isset( $submenu_file ) ) { if ( $submenu_file == $sub_item[2] ) $class[] = 'current'; ... } 
+1
source share

All Articles