Adding an options page to the WordPress admin menu with add_options_page

The add_options_page function in WordPress is used to add a new item to the settings menu in the WordPress admin dashboard. This function allows developers to create a custom settings page where various options and configurations related to a plugin or theme can be managed.

When invoked, add_options_page registers a new submenu item under the “Settings” menu. This submenu item links to a page where users can view and modify settings specific to the plugin or theme. The function facilitates the organization of settings by placing them within the existing administrative structure of WordPress.

By using add_options_page, developers can ensure that their settings pages are accessible in a consistent location, enhancing the overall user experience within the WordPress admin interface.

Parameters

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

Return Value

The function returns a string representing the resulting page’s hook_suffix, or false if the user does not have the required capability.

Examples

How to Add a Simple Options Page in WordPress

function my_simple_options_page() {
 add_options_page(
 'My Simple Options Page', // $page_title
 'Simple Options', // $menu_title
 'manage_options', // $capability
 'simple-options', // $menu_slug
 'my_simple_options_page_html' // $callback
 );
}
add_action('admin_menu', 'my_simple_options_page');

function my_simple_options_page_html() {
 if (!current_user_can('manage_options')) {
 return;
 }
 echo '<h1>My Simple Options Page</h1>';
 echo '<p>Settings for my simple options page.</p>';
}

This code adds a new menu item called “Simple Options” under the “Settings” menu in the WordPress admin dashboard. When clicked, it displays a page with the title “My Simple Options Page” and a paragraph of text. The add_options_page function is hooked into the admin_menu action to ensure it is executed at the right time.

How to Add an Options Page with Custom Capability

function my_custom_capability_options_page() {
 add_options_page(
 'Custom Capability Options', // $page_title
 'Custom Options', // $menu_title
 'edit_posts', // $capability
 'custom-capability-options', // $menu_slug
 'my_custom_capability_page_html' // $callback
 );
}
add_action('admin_menu', 'my_custom_capability_options_page');

function my_custom_capability_page_html() {
 if (!current_user_can('edit_posts')) {
 return;
 }
 echo '<h1>Custom Capability Options Page</h1>';
 echo '<p>Settings for users with custom capabilities.</p>';
}

This code adds a new menu item called “Custom Options” under the “Settings” menu in the WordPress admin dashboard. The page is only accessible to users who have the edit_posts capability. When clicked, it displays a page with the title “Custom Capability Options Page” and a paragraph of text.

How to Add an Options Page with a Specific Menu Position

function my_positioned_options_page() {
 add_options_page(
 'Positioned Options Page', // $page_title
 'Positioned Options', // $menu_title
 'manage_options', // $capability
 'positioned-options', // $menu_slug
 'my_positioned_options_page_html', // $callback
 99 // $position
 );
}
add_action('admin_menu', 'my_positioned_options_page');

function my_positioned_options_page_html() {
 if (!current_user_can('manage_options')) {
 return;
 }
 echo '<h1>Positioned Options Page</h1>';
 echo '<p>Settings for the positioned options page.</p>';
}

This code adds a new menu item called “Positioned Options” under the “Settings” menu in the WordPress admin dashboard. The menu item appears at position 99 in the menu order. When clicked, it displays a page with the title “Positioned Options Page” and a paragraph of text.

Conclusion

The add_options_page function in WordPress is designed to facilitate the addition of new options pages to the WordPress admin dashboard, specifically under the Settings menu. This function allows developers to create custom settings pages where users can manage various configuration options for plugins or themes. By employing add_options_page, developers can enhance the user experience by providing a centralized location for managing settings, thereby extending the functionality and customization capabilities of their WordPress projects.

Related WordPress Functions