Skip to main content
add solution to address problem about highlighting a menu item in place of the hidden page
Source Link
Stephen Harris
  • 32.7k
  • 6
  • 85
  • 118

From the docs on add_submenu_page(), you see that you can hide your submenu link from a top level menu item to which it belongs be setting the slug (1st argument) to null:

add_action( 'admin_menu', 'register_my_custom_submenu_page' ); function register_my_custom_submenu_page() { add_submenu_page( null, 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback', ); } 

To highlight the desired menu item (e.g. 'all charts' when accessing the hidden 'edit chart' page), you can do the following:

add_filter( 'submenu_file', function($submenu_file){ $screen = get_current_screen(); if($screen->id === 'id-of-page-to-hide'){ $submenu_file = 'id-of-page-to-higlight'; } return $submenu_file; }); 

From the docs on add_submenu_page(), you see that you can hide your submenu link from a top level menu item to which it belongs be setting the slug (1st argument) to null:

add_action( 'admin_menu', 'register_my_custom_submenu_page' ); function register_my_custom_submenu_page() { add_submenu_page( null, 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback', ); } 

From the docs on add_submenu_page(), you see that you can hide your submenu link from a top level menu item to which it belongs be setting the slug (1st argument) to null:

add_action( 'admin_menu', 'register_my_custom_submenu_page' ); function register_my_custom_submenu_page() { add_submenu_page( null, 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback', ); } 

To highlight the desired menu item (e.g. 'all charts' when accessing the hidden 'edit chart' page), you can do the following:

add_filter( 'submenu_file', function($submenu_file){ $screen = get_current_screen(); if($screen->id === 'id-of-page-to-hide'){ $submenu_file = 'id-of-page-to-higlight'; } return $submenu_file; }); 
Fixed code intention, comma styles and Docs link
Source Link
kaiser
  • 51k
  • 27
  • 152
  • 246

i came here with similar problem...

well, from theirFrom the docs -on https://codex.wordpress.org/Function_Reference/add_submenu_pageadd_submenu_page()

To, you see that you can hide your submenu link from a top level menu item to which it belongs you would instead do,be setting the slug (1st argument) to null:

add_action( 'admin_menu', 'register_my_custom_submenu_page' ); function register_my_custom_submenu_page() {    add_submenu_page( null null,  //or 'options.php'    , 'My Custom Submenu Page' , ,  'My Custom Submenu Page', ,  'manage_options', ,  'my-custom-submenu-page', ,  'my_custom_submenu_page_callback',  ); } 

}

So the first param that tells parent slug should be null... :) it worked for me...

i came here with similar problem...

well, from their docs - https://codex.wordpress.org/Function_Reference/add_submenu_page

To hide your submenu link from a top level menu item to which it belongs you would instead do,

add_action('admin_menu', 'register_my_custom_submenu_page'); function register_my_custom_submenu_page() {   add_submenu_page( null //or 'options.php'    , 'My Custom Submenu Page'  , 'My Custom Submenu Page' , 'manage_options' , 'my-custom-submenu-page' , 'my_custom_submenu_page_callback' ); 

}

So the first param that tells parent slug should be null... :) it worked for me...

From the docs on add_submenu_page(), you see that you can hide your submenu link from a top level menu item to which it belongs be setting the slug (1st argument) to null:

add_action( 'admin_menu', 'register_my_custom_submenu_page' ); function register_my_custom_submenu_page() {  add_submenu_page( null,  'My Custom Submenu Page',   'My Custom Submenu Page',   'manage_options',   'my-custom-submenu-page',   'my_custom_submenu_page_callback',  ); } 
Source Link
lanicor
  • 421
  • 1
  • 4
  • 4

i came here with similar problem...

well, from their docs - https://codex.wordpress.org/Function_Reference/add_submenu_page

To hide your submenu link from a top level menu item to which it belongs you would instead do,

add_action('admin_menu', 'register_my_custom_submenu_page'); function register_my_custom_submenu_page() { add_submenu_page( null //or 'options.php' , 'My Custom Submenu Page' , 'My Custom Submenu Page' , 'manage_options' , 'my-custom-submenu-page' , 'my_custom_submenu_page_callback' ); 

}

So the first param that tells parent slug should be null... :) it worked for me...