Adding Menus to the WordPress Admin

This post covers how to add a menu to the WordPress admin using add_menu_page and add_submenu_page.

I will assume you already know your way around the standard WordPress admin menus, since you need to be comfortable managing content before any of this is useful.

Adding a top level menu

add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );

The parameters:

  • $page_title: the page title that goes in the <title> tag
  • $menu_title: the label shown in the menu list
  • $menu_slug: the URL slug for the page
  • $capability: the capability required to see and use the menu, see the roles and capabilities list
  • $function: the function called when you click the menu item
  • $icon_url: path to the menu icon
  • $position: where the menu sits, counted from the top

Positions of the built-in menus

2   Dashboard
4   Separator
5   Posts
10  Media
15  Links
20  Pages
25  Comments
59  Separator
60  Appearance
65  Plugins
70  Users
75  Tools
80  Settings
99  Separator

With those numbers you can drop a menu anywhere, before or after any existing WordPress menu.

For example, to add a Project menu right after Posts:

<?php
add_action('admin_menu', 'addProjectMenu');

function addProjectMenu(){
    add_menu_page (
            'Project',
            'Project',
            'manage_options',
            'project',
            'projectManagement',
            '',
            '6'
    );
}

function projectManagement(){
    echo '<h1>This is the project management page</h1>';
}

Note the admin_menu action hook. That is where menu registration has to happen.

Adding a submenu

add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );

The parameters:

  • $parent_slug: the slug of the parent menu
  • $page_title: the page title in the <title> tag
  • $menu_title: the label shown in the menu list
  • $capability: the capability required to use the menu
  • $menu_slug: the URL slug for the page
  • $function: the function called when you click the item

WordPress also gives you shorthand functions for adding a submenu under each of the built-in menus:

<?php
add_dashboard_page();   // submenu under Dashboard
add_posts_page();       // submenu under Posts
add_media_page();       // submenu under Media
add_pages_page();       // submenu under Pages
add_comments_page();    // submenu under Comments
add_theme_page();       // submenu under Appearance
add_plugins_page();     // submenu under Plugins
add_users_page();       // submenu under Users
add_management_page();  // submenu under Tools
add_options_page();     // submenu under Settings

They work the same way as adding a menu, just with a few different arguments. The reference for add_submenu_page has the full details.

Removing menus from the admin

Two functions handle removal:

remove_menu_page();     // remove a menu
remove_submenu_page();  // remove a submenu

Full details in the references for remove_menu_page and remove_submenu_page.

Between those functions you can add or remove any menu item you want. This post does not cover everything, but the developer reference links above fill in the rest.