Finally, I found a way to do this, which is not an ugly hack, does not require JS to highlight the desired menu item (and submenu item) and works for regular menus registered by plugins ( @Josh answer only works for custom message types).
Essentially, you just need to register your submenu in normal mode, but then connect to the 'submenu_file' filter to unregister it and, in addition, also set another element of the submenu to highlight.
function so3902760_wp_admin_menu() { // Register the parent menu. add_menu_page( __( 'Parent title', 'textdomain' ) , __( 'Parent', 'textdomain' ) , 'manage_options' , 'my_parent_slug' , 'display_my_menu' ); // Register the hidden submenu. add_submenu_page( 'my_parent_slug' // Use the parent slug as usual. , __( 'Page title', 'textdomain' ) , '' , 'manage_options' , 'my_hidden_submenu' , 'display_my_submenu' ); } add_action( 'admin_menu', 'so3902760_wp_admin_menu' ); function so3902760_wp_admin_submenu_filter( $submenu_file ) { global $plugin_page; $hidden_submenus = array( 'my_hidden_submenu' => true, ); // Select another submenu item to highlight (optional). if ( $plugin_page && isset( $hidden_submenus[ $plugin_page ] ) ) { $submenu_file = 'submenu_to_highlight'; } // Hide the submenu. foreach ( $hidden_submenus as $submenu => $unused ) { remove_submenu_page( 'my_parent_slug', $submenu ); } return $submenu_file; } add_filter( 'submenu_file', 'so3902760_wp_admin_submenu_filter' );
JD Nov 30 '17 at 15:54 2017-11-30 15:54
source share