Function and usage of add_management_page() in wordpress

Question

Answers ( 1 )

    0
    2024-01-05T02:25:35+00:00

    The add_management_page function in WordPress is used to add a submenu page to the 'Tools' main menu in the WordPress admin dashboard. This function is essential for plugin developers who want to create custom tools and settings pages within the WordPress admin area. Here's a detailed explanation of the function and its usage:

    Function Signature

    add_management_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $callback = '', int $position = null ): string|false
    

    Function Description

    • Purpose: Adds a submenu page under the 'Tools' main menu in the WordPress admin dashboard.
    • Capability Check: The function requires a capability parameter, which determines whether the page is accessible to the user. The callback function must also check that the user has the required capability.
    • Return Value: Returns the resulting page's hook_suffix if the user has the necessary capability, otherwise returns false.

    Parameters

    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 item in the admin dashboard.
    3. $capability (string, required): The capability required for this menu to be displayed. It determines user access permissions.
    4. $menu_slug (string, required): The slug name to refer to this menu, which should be unique for this menu.
    5. $callback (callable, optional): The function called to output the content for this page. Default is an empty string.
    6. $position (int, optional): The position in the menu order where this item should appear. Default is null.

    Sample Usage

    Here’s an example of how add_management_page can be used in a WordPress plugin:

    function my_custom_tool_page() {
        // Page content goes here
        echo '<h1>My Custom Tool</h1>';
        echo '<p>This is a custom tool page.</p>';
    }
    
    function register_my_custom_tool_page() {
        add_management_page(
            'My Custom Tool',       // Page title
            'Custom Tool',          // Menu title
            'manage_options',       // Capability
            'my-custom-tool',       // Menu slug
            'my_custom_tool_page'   // Callback function
        );
    }
    
    add_action('admin_menu', 'register_my_custom_tool_page');
    

    In this example:

    • We define a callback function my_custom_tool_page that outputs the content for the custom tool page.
    • We use add_management_page inside the register_my_custom_tool_page function to add a submenu item under 'Tools'.
    • We hook register_my_custom_tool_page to the admin_menu action to ensure our custom page is added when the admin menu is built.

    The add_management_page function is a powerful tool for extending WordPress's admin functionality, allowing developers to create custom settings and tools pages tailored to their specific needs.

Leave an answer