0
0
WordpressHow-ToBeginner · 4 min read

How to Use add_options_page in WordPress to Add Admin Menu Pages

Use add_options_page in WordPress to add a new submenu page under the Settings menu in the admin dashboard. It requires a page title, menu title, capability, menu slug, and a callback function to display the page content. Hook this function into admin_menu action to register the page.
📐

Syntax

The add_options_page function adds a submenu page under the Settings menu in WordPress admin. It takes five parameters:

  • page_title: The title shown on the page itself.
  • menu_title: The title shown in the Settings menu.
  • capability: The user capability required to access this page (e.g., 'manage_options').
  • menu_slug: A unique slug identifier for the menu page.
  • function: The callback function that outputs the content of the page.
php
add_options_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function );
💻

Example

This example adds a new settings page called "My Plugin Settings" under the Settings menu. The page content shows a simple heading.

php
<?php
function my_plugin_settings_page() {
    echo '<div class="wrap"><h1>My Plugin Settings</h1><p>Welcome to the settings page.</p></div>';
}

function my_plugin_add_settings_page() {
    add_options_page(
        'My Plugin Settings',    // Page title
        'My Plugin',             // Menu title
        'manage_options',        // Capability
        'my-plugin-settings',    // Menu slug
        'my_plugin_settings_page'// Callback function
    );
}
add_action('admin_menu', 'my_plugin_add_settings_page');
Output
In WordPress admin dashboard, under Settings menu, a new submenu "My Plugin" appears. Clicking it shows a page with heading "My Plugin Settings" and a welcome message.
⚠️

Common Pitfalls

  • Not hooking add_options_page inside the admin_menu action will prevent the page from appearing.
  • Using a capability that the current user does not have will hide the menu item.
  • Forgetting to create the callback function or having a typo in its name causes a blank page or errors.
  • Using a non-unique menu_slug can cause conflicts with other plugins.
php
<?php
// Wrong: calling add_options_page outside admin_menu hook
add_options_page('Title', 'Menu', 'manage_options', 'slug', 'callback');

// Right: inside admin_menu hook
add_action('admin_menu', function() {
    add_options_page('Title', 'Menu', 'manage_options', 'slug', 'callback');
});
📊

Quick Reference

ParameterDescriptionExample
page_titleTitle shown on the settings page'My Plugin Settings'
menu_titleTitle shown in the Settings menu'My Plugin'
capabilityUser capability required to see the menu'manage_options'
menu_slugUnique slug for the menu page'my-plugin-settings'
functionCallback function to display page content'my_plugin_settings_page'

Key Takeaways

Always hook add_options_page inside the admin_menu action to register your settings page.
Use a capability like 'manage_options' to restrict access to admins.
Provide a unique menu_slug to avoid conflicts with other plugins.
Create a callback function that outputs the HTML content for your settings page.
The new page appears as a submenu under the Settings menu in the WordPress admin.