do_action( ‘admin_menu’, string $context )

Fires before the administration menu loads in the admin.

Parameters

$contextstring
Empty context.

More Information

  • This action is used to add extra submenus and menu options to the admin panel’s menu structure. It runs after the basic admin panel menu structure is in place.
  • This action mustn’t be placed in an admin_init action function because the admin_init action is called after admin_menu.

Source

do_action( 'admin_menu', '' );

Changelog

VersionDescription
1.5.0Introduced.

User Contributed Notes

  1. Skip to note 6 content

    If you want to change a menu label, you can hook this action and change the global $menu, $submenu values,

    add_action( 'admin_menu', 'change_media_label' );
    function change_media_label(){
      global $menu, $submenu;
      debug_msg($menu);
      $menu[10][0] = 'Photos/Videos';
      $submenu['upload.php'][5][0] = 'All Photos/Videos';
      $submenu['upload.php'][10][0] = 'Upload new';
    }
  2. Skip to note 7 content

    This snippet add admin menu and submenu HTML content

    //call the 'add_menu_page' function with 'admin_menu' action hook
    	add_action( 'admin_menu', array( $this, 'wpdocs_add_menu_page' ), 99 );
    
        /**
         * Add page to admin menu
         */
        public function wpdocs_add_menu_page() {
            add_menu_page(
                esc_html__( 'WooCommerce B2B Sales Agents', 'woocommerce-b2b-sales-agents' ),
                esc_html__( 'WooCommerce B2B Sales Agents', 'woocommerce-b2b-sales-agents'),
                'manage_woocommerce',
                'wcb2bsa-commissions',
                null,
                'dashicons-businessman',
                55.5
            );
            add_submenu_page(
                'wcb2bsa-commissions',
                esc_html__( 'Commissions', 'woocommerce-b2b-sales-agents' ),
                esc_html__( 'Commissions', 'woocommerce-b2b-sales-agents' ),
                'manage_woocommerce',
                'wcb2bsa-commissions',
                array( $this, 'wpdocs_add_menu_page_callback' )
            );
        }
    
        /**
         * Add page to admin menu callback
         */
        public function wpdocs_add_menu_page_callback() {
            include WCB2BSA_ABSPATH . 'includes/views/html-admin--page-commissions.php';
        }
  3. Skip to note 8 content

    Let’s say you’re building a plugin and you need to register an overview or dashboard menu option for your plugin page, you can do it like so:

    // Define constants
    define( 'PLUGIN_SLUG', 'your-plugin' );
    define( 'PLUGIN_ROLE', 'manage_options' );
    define( 'PLUGIN_DOMAIN', 'your-plugin-text-domain' );
    
    add_action( 'admin_menu', 'register_your_plugin_menu', 9 );
    
    function register_your_plugin_menu() {
    	add_menu_page(
    		__( 'Your Plugin', PLUGIN_DOMAIN ),
    		'Your Plugin',
    		PLUGIN_ROLE,
    		PLUGIN_SLUG,
    		false,
    		'dashicons-admin-generic',
    		''
    	);
    
    	add_submenu_page(
    		PLUGIN_SLUG,
    		'Your Plugin',
    		'Dashboard',
    		PLUGIN_ROLE,
    		PLUGIN_SLUG,
    		'your_plugin_dashboard_callback',
    	);
    }
  4. Skip to note 9 content

    You may come across the fact that deleting items having the slug like admin.php?page=jetpack in the admin_menu hook does not affect the menu in any way and the items remain in their places. To fix it, define correct slugs to delete with remove_menu_page() function.

    Try this code:

    function wpdocs_list_menus() {
        global $menu;
        var_dump( $menu );
    }
    add_action( 'admin_menu', 'wpdocs_list_menus', 99999 );
  5. Skip to note 10 content

    Example migrated from Codex:

    The example comes from the wpautop-control plugin, in which the code is used to add an options page to the “Settings” menu.

    add_action('admin_menu', 'wpautop_control_menu');
    
    function wpautop_control_menu() {
      add_submenu_page('options-general.php', 'wpautop-control', 'wpautop control', 'manage_options', 'wpautop-control-menu', 'wpautop_control_options');
    }

You must log in before being able to contribute a note or feedback.