Function and usage of add_options_page() in wordpress

Question

Answers ( 1 )

    0
    2024-01-05T02:37:38+00:00

    The add_options_page() function in WordPress is a powerful tool for developers to add a submenu page to the 'Settings' main menu in the WordPress admin dashboard. Here's a detailed explanation of its usage and functionality:

    Function:

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

    Description:

    This function allows you to add a new submenu page under the 'Settings' main menu in WordPress. The visibility of this submenu is controlled by a capability check, ensuring that only users with the appropriate permissions can view and interact with this menu.

    Parameters:

    1. $page_title (string, required):

      • This is the text displayed in the title tags of the page when the menu is selected.
      • It's what appears in the browser tab and in the header of the admin page.
    2. $menu_title (string, required):

      • This text is used for the actual menu item in the WordPress dashboard.
      • It's what you see in the 'Settings' menu list.
    3. $capability (string, required):

      • Specifies the capability required for this menu to be displayed to the user.
      • It's a way to control access, ensuring only users with this capability (like 'manage_options') can see the menu.
    4. $menu_slug (string, required):

      • This is a unique identifier for the menu.
      • It's used in URLs to represent this specific settings page.
    5. $callback (callable, optional):

      • A function that outputs the content for this page.
      • This is where you add your settings form or any other content for the page.
      • Default is an empty string, which means no callback function.
    6. $position (int, optional):

      • Determines the position of the menu item in the menu order.
      • Default is null, which lets WordPress decide the position.

    Return Value:

    • The function returns the resulting page’s hook_suffix, which can be used to further manipulate the page (like adding scripts), or false if the user does not have the required capability.

    Sample Usage:

    function my_custom_options_page() {
        add_options_page(
            'My Custom Settings', // Page Title
            'Custom Settings',    // Menu Title
            'manage_options',     // Capability
            'my-custom-settings', // Menu Slug
            'my_custom_page_callback' // Callback Function
        );
    }
    
    function my_custom_page_callback() {
        echo '<h1>My Custom Settings Page</h1>';
        // Add your settings form or other page contents here
    }
    
    add_action('admin_menu', 'my_custom_options_page');
    

    In this example, a new submenu item "Custom Settings" is added under 'Settings'. The page titled "My Custom Settings" is only accessible to users with the 'manage_options' capability. The callback function my_custom_page_callback is used to display the content of the settings page.

Leave an answer