Paulund
2012-09-07 #wordpress

Add New Menu Items To WordPress Admin

If you are a plugin or theme developer then you will get to the stage where you need to add a link to your plugin or theme settings page in your Wordpress admin area. The settings page will allow you your user to customise the plugin or theme in anyway they want. There are different ways you can add links to your plugin or theme settings page. Some people like to put links to the theme settings under the appearance tab, some people prefer to create a brand new menu item in the Wordpress admin area just for your theme. If you are creating a theme I think it's better to add your link to your theme settings page under your appearance tag. This is because a theme is used for the appearance of your site and all the settings should be under the appearance tab. If you are creating a plugin then you have more freedom to where you put a link to your settings screen, the choices are creating a brand new menu item just for your plugin, creating a link under the settings tab or just having a link on the plugin menu. In this article we are going to investigate the different areas we can add links to our settings pages by creating new menus in the Wordpress admin area.

Add Top Level Menu Items

If your Wordpress theme or plugin needs it's own menu item then you need to add a top level menu to your Wordpress admin area. To add a top level menu you need to use the Wordpress function add_menu_page().

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

  • $page_title - The title used on the settings page.
  • $menu_title - The title used on the menu.
  • $capability - Only displays the menu if the user matches this capability.
  • $menu_slug - The unique name of the menu slug.
  • $function - This is the callback function to run to display the page.
  • $icon_url - Display a icon just for the menu.
  • $position - This allows you to choose when the menu item appears in the list.

These are the positions of the menu items on the admin screen.

 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

If two menu items have the same position then one of them will be overwritten so only one item is displayed.

Add Menu Under Top Level Items

These snippets will show you how you can create a new link under any of the top default menus in Wordpress. To add sub-level menus all you have to do is use the add_submenu_page() function and pass in the parent node as the first parameter.

<?php add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function); ?>

  • $parent_slug - This is the parent slug of the top level menu item.
  • $page_title - The title to use on the page when the user clicks the link.
  • $menu_title - The title of the menu.
  • $capability - The capability of the user to display the menu.
  • $menu_slug - The slug used for the menu.
  • $function - This is the callback function to display the linked to page.

This is how to use the add_submenu_page() function, the most important argument of this function is the $parent_slug, this will decide where to place the submenu. All you need to do is pass in the parent_slug the file.php of the page you want to place the menu under.

Add Under Dashboard Menu

To add a menu item under the dashboard item.

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

function add_dashboard_menu(){
     add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function); 
}
?>

All the default menu items in Wordpress come with there own function to add a sub menu item, which makes it even easier to add a sub menu. To add a menu to the dashboard you can use the function add_dashboard_page().

<?php 

add_action('admin_menu', 'add_dashboard_menu');

function add_dashboard_menu(){
     add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function); 
}

?>

Add Under Posts Menu

To add a menu item under the post item.


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

function add_post_menu(){
     add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function); 
}
?>

Or you can use the add to the posts page by using the add_posts_page() function.


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

function add_post_menu(){
     add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function); 
}
?> 

Add Under Media Menu

To add a menu item under the media item.


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

function add_media_menu(){
     add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function);
}
?>

Or you can add to the media menu by using the add_media_menu() function.


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

function add_media_menu(){
     add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function);
}
?> 

Add Under Links Menu

To add a menu item under the links item.


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

function add_links_menu(){
     add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function); 
}
?>

Or you can use the add_links_page() function.


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

function add_links_menu(){
     add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function);
}
?> 

Add Under Pages Menu

To add a menu item under the pages item.


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

function add_pages_menu(){
     add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function); 
}
?>

Or you can use the function add_pages_page().


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

function add_pages_menu(){
     add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function);
}
?> 

Add Under Comments Menu

To add a menu item under the comments item.


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

function add_comments_menu(){
     add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function); 
}
?>

Or you can use the add_comments_page() function.


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

function add_comments_menu(){
     add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function);
}
?> 

Add Under Custom Post Types Menu

To add a menu item under your custom post type item.


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

function add_custom_post_menu(){
     add_submenu_page( 'edit.php?post_type=your_post_type', $page_title, $menu_title, $capability, $menu_slug, $function); 
}
?>

Because custom post types aren't default Wordpress menus there is no inbuilt function to use to add sub-menus.

Add Under Appearance Menu

To add a menu item under the appearance item.


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

function add_appearance_menu(){
     add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function); 
}
?>

Or you can use the function add_theme_page() to add the theme settings page.


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

function add_appearance_menu(){
     add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function);
}
?> 

Add Under Plugins Menu

To add a menu item under the plugins item.


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

function add_plugins_menu(){
     add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function); 
}
?>

Or you can use the add_plugins_page() function.


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

function add_plugins_menu(){
     add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function);
}
?> 

Add Under Users Menu

To add a menu item under the users item.


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

function add_users_menu(){
     add_submenu_page( 'users.php', $page_title, $menu_title, $capability, $menu_slug, $function); 
}
?>

Or you can use the function add_users_page().


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

function add_users_menu(){
     add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function);
}
?> 

Add Under Tools Menu

To add a menu item under the tools item.


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

function add_tools_menu(){
    add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function); 
}
?>

Or you can use the add_management_page() function.


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

function add_tools_menu(){
     add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function );
}
?> 

Add Under Settings Menu

To add a menu item under the settings item.


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

function add_settings_menu(){
     add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function);
} 
?>

Or you use the add_options_page() function.


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

function add_settings_menu(){
     add_options_page('My Plugin Options', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options');
}
?>