The add_pages_page() WordPress PHP function adds a submenu page to the Pages main menu.
Usage
This function is typically used to add a custom submenu to the main Pages menu in WordPress admin. Here’s an example:
add_pages_page('My Custom Page', 'My Page', 'manage_options', 'my_custom_page', 'display_my_custom_page');
This would add a new submenu item labeled ‘My Page’ under the main Pages menu. When clicked, it would call the display_my_custom_page()
function to generate the content of the page.
Parameters
- $page_title (string) – The text to be displayed in the title tags of the page when the menu is selected.
- $menu_title (string) – The text to be used for the menu.
- $capability (string) – The capability required for this menu to be displayed to the user.
- $menu_slug (string) – The slug name to refer to this menu by (should be unique for this menu).
- $callback (callable, optional) – The function to be called to output the content for this page. Default is an empty string.
- $position (int, optional) – The position in the menu order this item should appear. Default is null.
More information
See WordPress Developer Resources: add_pages_page()
This function is not deprecated and can be found in the wp-admin/includes/plugin.php file.
Examples
Add a Simple Page
This example adds a simple page to the Pages menu.
// Define the output function function display_simple_page() { echo 'Hello, this is my simple page!'; } // Add the page to the Pages menu add_pages_page('My Simple Page', 'Simple Page', 'manage_options', 'my_simple_page', 'display_simple_page');
Add a Page with Custom Position
This example adds a page at a specific position in the Pages menu.
// Define the output function function display_custom_position_page() { echo 'This page is at a custom position in the menu!'; } // Add the page to the Pages menu at position 5 add_pages_page('My Custom Position Page', 'Custom Position', 'manage_options', 'my_custom_position_page', 'display_custom_position_page', 5);
Add a Page with Custom Capability
This example adds a page that only users with ‘edit_posts’ capability can view.
// Define the output function function display_custom_capability_page() { echo 'Only users with edit_posts capability can see this page!'; } // Add the page to the Pages menu add_pages_page('My Custom Capability Page', 'Custom Capability', 'edit_posts', 'my_custom_capability_page', 'display_custom_capability_page');
Add a Page without a Callback Function
This example adds a page without specifying a callback function.
// Add the page to the Pages menu add_pages_page('My Page Without Callback', 'No Callback', 'manage_options', 'my_page_without_callback');
Add a Page with HTML Output
This example adds a page with HTML output.
// Define the output function function display_html_page() { echo '<h1>Welcome to my page!</h1><p>This is some content.</p>'; } // Add the page to the Pages menu add_pages_page('My HTML Page', 'HTML Page', 'manage_options', 'my_html_page', 'display_html_page');