Using WordPress ‘add_menu_page()’ PHP function

The add_menu_page() WordPress PHP function adds a top-level menu page to the WordPress admin dashboard. This function uses a capability parameter to decide whether the page should be included in the menu. The function handling the output of the page should verify that the user has the required capability.

Usage

Here’s a basic usage of the add_menu_page() function:

add_menu_page(
    'My Custom Page',    // Page title
    'Custom Page',       // Menu title
    'manage_options',    // Capability
    'custompage_slug',   // Menu slug
    'my_custom_page_function'  // Function to handle the output of the page
);

This will create a top-level menu page titled “My Custom Page” with the slug “custompage_slug”. The page content will be generated by the function my_custom_page_function.

Parameters

  • $page_title (string) Required – The text to be displayed in the title tags of the page when the menu is selected.
  • $menu_title (string) Required – The text to be used for the menu.
  • $capability (string) Required – The capability required for this menu to be displayed to the user.
  • $menu_slug (string) Required – The slug name to refer to this menu by. It should be unique for this menu page and only include lowercase alphanumeric, dashes, and underscores characters to be compatible with sanitize_key().
  • $callback (callable) Optional – The function to be called to output the content for this page. Default: ''
  • $icon_url (string) Optional – The URL to the icon to be used for this menu. This can be a base64-encoded SVG using a data URI, a Dashicons helper class, or ‘none’. Default: ''
  • $position (int|float) Optional – The position in the menu order this item should appear. Default: null

More Information

See WordPress Developer Resources: add_menu_page()

Examples

Adding a Custom Menu Page

This example creates a top-level menu page titled “My Custom Page”. The page content is generated by the my_custom_page_function function.

function my_custom_menu_page() {
    echo "Welcome to my custom page!";
}

add_action('admin_menu', function() {
    add_menu_page(
        'My Custom Page',    // Page title
        'Custom Page',       // Menu title
        'manage_options',    // Capability
        'custompage_slug',   // Menu slug
        'my_custom_menu_page'  // Function to output the content
    );
});

Setting a Custom Icon

This example demonstrates how to set a custom icon for the menu page. Here, we’re using a Dashicon class.

add_menu_page(
    'My Custom Page',
    'Custom Page',
    'manage_options',
    'custompage_slug',
    'my_custom_menu_page',
    'dashicons-heart'  // Using a Dashicon as the menu icon
);

Specifying Menu Position

This example demonstrates how to specify the position of the menu page in the admin menu. Here, we’re placing our menu page at position 5.

add_menu_page(
    'My Custom Page',
    'Custom Page',
    'manage_options',
    'custompage_slug',
    'my_custom_page_function',
    'dashicons-heart',
    5  // Position of the menu item in the admin menu
);