How do you add a WordPress admin page without adding it to the menu?

I have finally discovered a way to do this that isn't an ugly hack, doesn't require JS to highlight the desired menu item (and submenu item), and works for regular menus registered by plugins (@Josh's answer only works for custom post types).

Essentially, you just need to register your submenu normally, but then hook into the 'submenu_file' filter to deregister it and optionally also set another submenu item to highlight instead.

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

Best solution here http://wordpress.org/support/topic/add-backend-page-without-menu-item

use add_submenu_page with parent slug = null

Tags:

Wordpress