If you’re working with WordPress and want to add custom settings pages to your plugin or theme, then the add_options_page() function is what you need. This function adds a new sub-menu page to the Settings menu, allowing you to create a custom options page with your own fields and settings.
The add_options_page() function accepts 4 parameters:
- $page_title: The title of the options page.
- $menu_title: The title of the menu item that appears in the WordPress dashboard.
- $capability: The minimum user capability required to access the options page.
- $menu_slug: The unique slug for the options page, used in the URL of the page.
Here’s an example usage code:
function myplugin_add_options_page() {
add_options_page(
'MyPlugin Settings',
'MyPlugin',
'manage_options',
'myplugin-settings',
'myplugin_render_options_page'
);
}
function myplugin_render_options_page() {
// Output your options page HTML here
}
add_action( 'admin_menu', 'myplugin_add_options_page' );
In this example, we’re creating an options page for a plugin called MyPlugin. The add_options_page() function is called inside a function hooked to the admin_menu action, which ensures the options page is added to the Settings menu.
The function also specifies the minimum user capability required to access the options page (manage_options), and provides a unique slug for the page (myplugin-settings). Finally, we specify a callback function (myplugin_render_options_page) to output the HTML for the options page.
Overall, the add_options_page() function is a powerful tool for creating custom settings pages in WordPress.