Wordpress - Add An Admin Page, But Don't Show It On The Admin Menu


Answer :

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; }); 

Use a submenu page as parent slug. The admin menu has just two levels, so the imaginary third level will be hidden.

Sample code, tested:

add_action( 'admin_menu', 'wpse_73622_register_hidden_page' );  function wpse_73622_register_hidden_page() {     add_submenu_page(         'options-writing.php',         'Hidden!',         'Hidden!',         'exists',         'wpse_73622',         'wpse_73622_render_hidden_page'     );     # /wp-admin/admin.php?page=wpse_73622 }  function wpse_73622_render_hidden_page() {     echo '<p>hello world</p>'; } 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?