The admin_menu WordPress PHP action fires before the administration menu loads in the admin, allowing you to add extra submenus and menu options to the admin panel’s menu structure.
Usage
add_action('admin_menu', 'your_function_name'); function your_function_name() { // your custom code here }
Parameters
$context
(string): Empty context.
More information
See WordPress Developer Resources: admin_menu
Important: Do not place this action in an admin_init
action function, as admin_init
is called after admin_menu
.
Examples
Add a Top-Level Menu Item
To add a new top-level menu item to the admin menu:
add_action('admin_menu', 'add_my_custom_menu'); function add_my_custom_menu() { add_menu_page( 'My Custom Menu', // Page title 'My Custom Menu', // Menu title 'manage_options', // Capability 'my-custom-menu', // Menu slug 'my_custom_menu_output' // Function ); } function my_custom_menu_output() { echo 'Hello, this is my custom menu page!'; }
Add a Submenu Item
To add a new submenu item under an existing top-level menu item:
add_action('admin_menu', 'add_my_custom_submenu'); function add_my_custom_submenu() { add_submenu_page( 'my-custom-menu', // Parent menu slug 'My Custom Submenu', // Page title 'My Custom Submenu', // Menu title 'manage_options', // Capability 'my-custom-submenu', // Menu slug 'my_custom_submenu_output' // Function ); } function my_custom_submenu_output() { echo 'Hello, this is my custom submenu page!'; }
Add a Separator in the Admin Menu
To add a separator between menu items:
add_action('admin_menu', 'add_my_custom_separator'); function add_my_custom_separator() { global $menu; $menu[99] = array( '', 'read', 'separator-custom', '', 'wp-menu-separator' ); }
Remove a Menu Item
To remove an existing menu item:
add_action('admin_menu', 'remove_my_custom_menu', 999); function remove_my_custom_menu() { remove_menu_page('my-custom-menu'); // Menu slug }
Change the Order of Menu Items
To change the order of menu items:
add_action('admin_menu', 'reorder_my_custom_menus', 999); function reorder_my_custom_menus() { global $menu; $menu[25] = $menu[80]; // Move "Settings" menu unset($menu[80]); // Remove the original "Settings" menu position }