Function and usage of add_dashboard_page() in wordpress

Question

Answers ( 1 )

    0
    2024-01-05T02:16:50+00:00

    The add_dashboard_page() function in WordPress is used for adding a submenu page to the Dashboard main menu. It's particularly useful for creating custom pages within the WordPress admin area. Here's a detailed explanation of its usage:

    Function:

    • Purpose: Adds a submenu page to the Dashboard main menu in WordPress.

    Description:

    • This function is primarily used for adding custom pages to the WordPress Dashboard.
    • It requires a specific capability to ensure that only users with the appropriate permissions can access this menu page.
    • The function that handles the output of the page must also verify that the current user has the required capability.

    Parameters:

    1. $page_title (string, required):

      • Description: The title of the page, which appears in the title tags of the page when the menu is selected.
      • Example: 'My Custom Dashboard Page'
    2. $menu_title (string, required):

      • Description: The text that appears in the Dashboard menu for this page.
      • Example: 'Custom Page'
    3. $capability (string, required):

      • Description: The capability required for this menu to be displayed to the user. It's a way of ensuring that only users with certain permissions can see this menu item.
      • Example: 'manage_options'
    4. $menu_slug (string, required):

      • Description: A unique identifier for the menu. This slug is used in URLs and for hooking into the WordPress system.
      • Example: 'custom-dashboard-page'
    5. $callback (callable, optional):

      • Description: The function that outputs the content for this dashboard page. If not provided, the page will not output any content.
      • Default: ''
      • Example: 'my_custom_page_callback_function'
    6. $position (int, optional):

      • Description: Determines where in the menu order this item should appear. If not provided, it will be placed at the bottom of the menu items.
      • Default: null
      • Example: 6

    Return:

    • The function returns the resulting page’s hook_suffix, which is a string. This hook_suffix can be used to further manipulate the page, such as adding scripts or styles specifically for this page.
    • Returns false if the user does not have the capability required, meaning the page won't be added.

    Sample Usage:

    function add_my_custom_dashboard_page() {
        add_dashboard_page(
            'My Custom Dashboard Page', // $page_title
            'Custom Page',             // $menu_title
            'manage_options',          // $capability
            'custom-dashboard-page',   // $menu_slug
            'my_custom_page_callback', // $callback
            6                          // $position
        );
    }
    
    function my_custom_page_callback() {
        echo '<div>Welcome to my custom page!</div>';
    }
    
    add_action('admin_menu', 'add_my_custom_dashboard_page');
    

    In this example, a new submenu page is added to the Dashboard menu. It's only visible to users with the 'manage_options' capability. The page will display "Welcome to my custom page!" when accessed.

Leave an answer