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

Viewed 60255

I'm building a WordPress plugin and I'd like to have an edit-item page that can't be reached via the submenu (because then the item wouldn't be specified).

This resource (http://codex.wordpress.org/Adding_Administration_Menus) shows how to associate an admin page with a function, but not how to do so without adding it as a menu item.

Can this be done?

Thanks!

13 Answers

Create sub menu page and parent slug leave it empty like this:

// Create page were you can add new users.
    public function add_create_user_menu() {
        add_submenu_page(
            '',
            'Create User',
            'Create User',
            'manage_options',
            'create-user',
            array( $this, 'add_create_user_page' )
        );
    }

You can access it like this:

<a href="/wp-admin/admin.php?page=create-user">Add New</a>

It seems this need is still valid for nowadays version. I am using WordPress 5.3.2 and I used the following methods to remove the parent named menu from the submenu.

add_action( 'admin_menu', 'submenus' ); 

function submenus()
    {
        global $submenu;

        $parent_slug = 'parent_slug_name';

        // remove parent named menu from submenu because it is always the first one in the submenu array, so the offset is 0 and remove just 1
        array_splice( $submenu[$parent_slug], 0, 1 ); // with reindex
        // or unset( $submenu[$parent_slug][0] ); // without reindex
    }

Hope it helps others who want to achieve the same goal.

Edit / Added

Apart from submenu, parent menu could also be updated in similar way. For parent menu, the global variable is $menu. example for reference:

add_action( 'admin_menu', array( $this, 'modify_menu_title' ) );

function modify_menu_title() {
    global $menu;

    $page = 'some-page-slug';
    $new_menu_title = 'New Title Name';
    foreach( $menu as $key => $value ) {
        if( $menu[$key][2] === $page ) {
            $menu[$key][0] = $new_menu_title;
        }
    }
}
Related