How to use add_submenu_page and add_menu_page in WordPress
Published on: Jul 11, 2022
2 min read.
If you have made a plugin or theme for WordPress, you know the
add_menu_page is a function in WordPress that allows you to add a top-level menu item to the WordPress dashboard. It is generally used to create custom admin pages and is typically called from within a custom plugin or theme. It is very useful, and almost all active WordPress themes and plugins have a need for their own administrative page the WordPress dashboard.
But what happens if you need more than one page or you don’t want to clutter the already cluttered WordPress administration panel?
add_submenu_page is a function provided by WordPress that allows you to add a submenu page to an existing top-level menu in the WordPress admin area. It is most commonly used to create settings pages for plugins, but it can also be used to add custom pages to the WordPress dashboard.
Here is an example of how to use
add_submenu_page to create a custom submenu page:
In the example above, we used
add_submenu_page to add a submenu page under the “Settings” menu (which has the slug
options-general.php). The page has a title of “Custom Submenu Page”, a menu title of “Custom Submenu”, and a menu slug of
custom-submenu-page. When the submenu page is clicked, the
custom_submenu_page_callback function will be called to display the page content.
There are several arguments that you can pass to
add_submenu_page to customize the behavior of your submenu page:
$parent_slug(required): The slug of the parent menu page. This is typically the slug of an existing top-level menu page, such as
$page_title(required): The title of the submenu page. This will be displayed in the browser’s title bar and the page heading.
$menu_title(required): The text to be used for the menu. This will be visible in the WordPress admin menu.
$capability(required): The capability required for this menu to be displayed to the user.
$menu_slug(required): The slug name to refer to this menu by (should be unique for this menu).
$function(required): The function to be called to output the content for this page.
There are also several optional arguments that you can use to customize the behavior of your submenu page further. For example, you can use the
$position argument to specify the position of the submenu page in the menu. You can also use the
$icon_url argument to specify a custom icon for the submenu.
That’s it! With the
add_submenu_page function, you can easily add custom submenu pages to the WordPress admin area.