0
0
WordpressHow-ToBeginner · 3 min read

How to Add Menu Page in Admin in WordPress Easily

Use the add_menu_page function inside an admin_menu action hook to add a custom menu page in WordPress admin. This function requires parameters like page title, menu title, capability, menu slug, and a callback to display the page content.
📐

Syntax

The add_menu_page function adds a new top-level menu in the WordPress admin sidebar. It requires these parameters:

  • page_title: The title shown on the page.
  • menu_title: The label shown in the admin menu.
  • capability: The user permission needed to see the menu.
  • menu_slug: A unique slug for the menu page URL.
  • callback: A function that outputs the page content.
  • icon_url (optional): Icon for the menu.
  • position (optional): Position in the menu order.
php
add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $callback, string $icon_url = '', int $position = null );
💻

Example

This example adds a new menu page called "Custom Menu" in the WordPress admin. The page shows a simple message when clicked.

php
<?php
// Hook to admin_menu to add custom menu
add_action('admin_menu', function() {
    add_menu_page(
        'Custom Menu Page',      // Page title
        'Custom Menu',           // Menu title
        'manage_options',        // Capability
        'custom-menu-slug',      // Menu slug
        'custom_menu_page_html', // Callback function
        'dashicons-admin-generic', // Icon URL (dashicon)
        6                       // Position
    );
});

// Callback function to display content
function custom_menu_page_html() {
    echo '<div class="wrap"><h1>Welcome to Custom Menu Page</h1><p>This is your custom admin page content.</p></div>';
}
Output
In WordPress admin sidebar, a new menu item "Custom Menu" appears. Clicking it shows a page with heading "Welcome to Custom Menu Page" and a paragraph below.
⚠️

Common Pitfalls

  • Not hooking add_menu_page inside admin_menu action will prevent the menu from showing.
  • Using a capability that the current user does not have will hide the menu.
  • Forgetting to create the callback function causes a fatal error.
  • Using a non-unique menu_slug can cause conflicts with other plugins or themes.
php
<?php
// Wrong: Calling add_menu_page outside admin_menu hook
add_menu_page('Title', 'Menu', 'manage_options', 'slug', 'callback');

// Right: Hook inside admin_menu
add_action('admin_menu', function() {
    add_menu_page('Title', 'Menu', 'manage_options', 'slug', 'callback');
});
📊

Quick Reference

ParameterDescriptionExample
page_titleTitle shown on the menu page"Custom Menu Page"
menu_titleLabel shown in admin sidebar"Custom Menu"
capabilityUser permission to see menu"manage_options"
menu_slugUnique slug for URL"custom-menu-slug"
callbackFunction to display page content"custom_menu_page_html"
icon_urlIcon for menu (optional)"dashicons-admin-generic"
positionMenu order position (optional)6

Key Takeaways

Always add your menu page inside the admin_menu action hook.
Use a capability like manage_options to control who sees the menu.
Provide a unique menu_slug to avoid conflicts.
Create a callback function to output your menu page content.
Use dashicons or custom icons to visually identify your menu.