WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/add menu page

Contents

Description

Add a top level menu page.

Note: If you're running into the "You do not have sufficient permissions to access this page" error, then you've hooked too early. The hook you should use is admin_menu.

Specifically, creates a new top level menu section in the admin menu sidebar and registers a hook to callback your function for outputting the page content when the linked menu page is requested. Returns the $hookname.

Usage

<?php add_menu_page$page_title$menu_title$capability$menu_slug$function$icon_url$position ); ?>

Parameters

$page_title
(string) (required) The text to be displayed in the title tags of the page when the menu is selected
Default: None
$menu_title
(string) (required) The on-screen name text for the menu
Default: None
$capability
(string) (required) The capability required for this menu to be displayed to the user. User levels are deprecated and should not be used here!
Default: None
$menu_slug
(string) (required) The slug name to refer to this menu by (should be unique for this menu). Prior to Version 3.0 this was called the file (or handle) parameter. If the function parameter is omitted, the menu_slug should be the PHP file that handles the display of the menu page content.
Default: None
$function
(string) (optional) The function that displays the page content for the menu page.
Default: None. Technically, the function parameter is optional, but if it is not supplied, then WordPress will assume that including the PHP file will generate the administration screen, without calling a function. Most plugin authors choose to put the page-generating code in a function within their main plugin file. In the event that the function parameter is specified, it is possible to use any string for the file parameter. This allows usage of pages such as ?page=my_super_plugin_page instead of ?page=my-super-plugin/admin-options.php.
The function must be referenced in one of two ways:
  1. if the function is a member of a class within the plugin it should be referenced as array( $this, 'function_name' )
  2. in all other cases, using the function name itself is sufficient
$icon_url
(string) (optional) The icon for this menu.
Default: empty string
  • If you have a custom image file, you can use the plugin_dir_url( __FILE__ ) function to get the URL of your plugin directory and then add the image filename to it. Icons should be 20 x 20 pixels or smaller.
  • (WP 3.8+) If 'dashicons-...', a Dashicon is shown from the collection at http://melchoyce.github.io/dashicons/. For example, the default "gear" symbol could be explicitly specified with 'dashicons-admin-generic'.
  • (WP 3.8+) If 'data:image/svg+xml;base64...', the specified SVG data image is used as a CSS background.
  • If 'none' (previously 'div'), the icon is replaced with an empty div you can style with CSS.
  • If '' (default), the "gear" Dashicon is shown (and menu-icon-generic is added to the CSS classes of the link).
$position
(integer) (optional) The position in the menu order this menu should appear. By default, if this parameter is omitted, the menu will appear at the bottom of the menu structure. The higher the number, the lower its position in the menu. WARNING: if two menu items use the same position attribute, one of the items may be overwritten so that only one item displays! Risk of conflict can be reduced by using decimal instead of integer values, e.g. 63.3 instead of 63 (Note: Use quotes in code, IE '63.3').
Default: bottom of menu structure
Positions for Core Menu Items
 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

Return Values

string 
$hookname used internally to track menu page callbacks for outputting the page inside the global $menu array

Examples

Add a custom menu item to the WordPress admin menu, for a user with administrator capability:

Method 1:

add_action( 'admin_menu', 'register_my_custom_menu_page' );

function register_my_custom_menu_page(){
    add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'myplugin/myplugin-admin.php', '', plugins_url( 'myplugin/images/icon.png' ), 6 );
}

With this method the page-generating code should be located in myplugin/myplugin-admin.php:

<?php
    echo "Admin Page Test";
?>

Method 2:

add_action( 'admin_menu', 'register_my_custom_menu_page' );

function register_my_custom_menu_page(){
    add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'custompage', 'my_custom_menu_page', plugins_url( 'myplugin/images/icon.png' ), 6 ); 
}

function my_custom_menu_page(){
    echo "Admin Page Test";	
}

Notes

  • This function takes a 'capability' (see Roles and Capabilities) which will be used to determine whether or not a page is included in the menu. The function which is hooked in to handle the output of the page must check that the user has the required 'capability' as well.
  • If you are using the Settings API to save data, and need the user to be other than the administrator, will need to modify the permissions via the hook option_page_capability_{$option_page}, where $option_page which is the same as menu_slug in add_menu_page(). Example allowing an editor to save data:
// add page visible to editors
add_action( 'admin_menu', 'register_my_page' );
function register_my_page(){
    add_menu_page( 'My Page Title', 'My Page', 'edit_others_posts', 'my_page_slug', 'my_page_function', plugins_url( 'myplugin/images/icon.png' ), 6 ); 
}

// modify capability
function my_page_capability( $capability ) {
	return 'edit_others_posts';
}
add_filter( 'option_page_capability_my_page_slug', 'my_page_capability' );

Change Log

Source File

add_menu_page() is located in wp-admin/includes/plugin.php.

Related

Administration Menus: add_menu_page(), remove_menu_page(), add_submenu_page(), remove_submenu_page(), add_dashboard_page(), add_posts_page(), add_media_page(), add_links_page(), add_pages_page(), add_comments_page(), add_theme_page(), add_plugins_page(), add_users_page(), add_management_page(), add_options_page()

See also index of Function Reference and index of Template Tags.