Function and usage of add_menu_page() in wordpress


Answers ( 1 )


    The add_menu_page() function in WordPress is used to add a top-level menu page to the WordPress admin dashboard. This function is particularly useful when you're developing a plugin or a theme and need to add your own settings or features page to the WordPress admin area.

    Here's a detailed explanation of the function and its parameters:


    • Purpose: Adds a top-level menu page in the WordPress admin dashboard.


    • This function is used to create a new menu item in the WordPress admin menu. It requires specific capabilities to determine whether or not a page is included in the menu. The function hooked to this menu must also check that the user has the required capabilities.


    1. $page_title (string, required):
      • The text displayed in the title tags of the page when the menu is selected.
    2. $menu_title (string, required):
      • The text used for the menu.
    3. $capability (string, required):
      • The capability required for this menu to be displayed to the user. It's a way to control who can see and interact with this menu page based on user roles.
    4. $menu_slug (string, required):
      • The slug name to refer to this menu. It should be unique for this menu page and follow specific character rules for compatibility.
    5. $callback (callable, optional):
      • The function called to output the content for this page. If not set, it defaults to an empty string.
    6. $icon_url (string, optional):
      • The URL to the icon used for this menu. It can be a base64-encoded SVG, a Dashicons helper class, or 'none' for a CSS-based icon.
    7. $position (int|float, optional):
      • The position in the menu order where this item should appear. Defaults to null, which lets WordPress decide the position.


    • Type: string
    • Description: The resulting page’s hook_suffix, which can be used to further manipulate the page.

    Sample Usage:

        'My Custom Page', // Page title
        'Custom Menu',    // Menu title
        'manage_options', // Capability
        'custom-menu-slug', // Menu slug
        'my_custom_page_callback', // Callback function
        'dashicons-chart-pie', // Icon URL
        6 // Position
    function my_custom_page_callback() {
        // Code to display the content of your custom page
        echo '<h1>Welcome to My Custom Page</h1>';

    In this example, a new menu item titled "Custom Menu" is added. It is visible to users with the 'manage_options' capability (typically administrators). The menu has a pie chart icon from Dashicons, and it appears in the sixth position in the admin menu. The content of the page is handled by the my_custom_page_callback function.

Leave an answer