Using WordPress ‘add_options_page()’ PHP function

The add_options_page() WordPress PHP function is used to add a new submenu page under the Settings main menu in your WordPress admin dashboard.

Usage

Here’s a basic usage example of the add_options_page() function:

add_options_page('My Page Title', 'My Menu Title', 'manage_options', 'my-menu-slug', 'my_page_content');

In this example, the function creates a new submenu page titled “My Page Title”. The title of the menu in the Settings main menu will be “My Menu Title”. The ‘manage_options’ capability means only administrators can see this menu. ‘my-menu-slug’ is a unique identifier for the menu, and ‘my_page_content’ is the function that will output the content of the page.

Parameters

  • $page_title (string) (required): The text to be displayed in the title tags of the page when the menu is selected.
  • $menu_title (string) (required): The text to be used for the menu.
  • $capability (string) (required): The capability required for this menu to be displayed to the user.
  • $menu_slug (string) (required): The slug name to refer to this menu by. This should be unique for this menu.
  • $callback (callable) (optional): The function to be called to output the content for this page. Default: ”.
  • $position (int) (optional): The position in the menu order this item should appear. Default: null.

More information

See WordPress Developer Resources: add_options_page()

This function should be hooked to admin_menu. Hooking it too early, such as to admin_init, can cause an insufficient permissions error.

Examples

Basic Menu Item

In this example, we are adding a basic menu item to the Settings menu. The page content function simply outputs a string.

// Function to output page content
function my_page_content() {
    echo 'Hello, this is my plugin page!';
}

// Add the menu item
add_options_page('My Plugin Page', 'My Plugin', 'manage_options', 'my-plugin', 'my_page_content');

This example includes a capability check to ensure the user can manage options.

// Function to output page content
function my_page_content() {
    if (current_user_can('manage_options')) {
        echo 'Welcome, admin!';
    } else {
        echo 'Sorry, you do not have permission to view this page.';
    }
}

// Add the menu item
add_options_page('My Plugin Page', 'My Plugin', 'manage_options', 'my-plugin', 'my_page_content');

Using within a Class

This example demonstrates how to use add_options_page() within a class.

class MyPlugin_Options_Page {
    function __construct() {
        add_action('admin_menu', array($this, 'add_options_page'));
    }

    function add_options_page() {
        add_options_page('Page Title', 'My Plugin', 'manage_options', 'my-plugin', array($this, 'display_page'));
    }

    function display_page() {
        echo 'This is my plugin settings page!';
    }
}

new MyPlugin_Options_Page();

Specifying Menu Position

This example shows how to specify the position of the menu item.

function my_page_content() {
    echo 'This is my plugin page!';
}

add_options_page('My Plugin Page', 'My Plugin', 'manage_options',