Anatomy Of A WordPress Plugin: Adding Admin Submenus For Options Pages

WordPressI have not yet written a plugin for WordPress that is complex enough to require the settings to be broken down over multiple pages in the Admin area, but this is something that is possible to do fairly easily.

In the last post we covered adding a link on the menu using the add_options_page function.

This function works for a single options page, but this function doesn’t work for submenus.

Instead we need to use add_menu_page to add the top level menu and add_submenu_page.

As with add_options_page, these functions are called using the add_action function with the admin_menu hook to call a custom function.

The code below shows the two functions being called for a mocked up example:

add_action("admin_menu", "azc_create_menus");

function azc_create_menus() {
    add_menu_page( "azurecurve Menu"
			, "azurecurve Menu"
			, 0
			, "azurecurve-menu-slug"
			, "azurecurveMenuPageFunction"
			, plugins_url( '/images/favicon.ico', __FILE__ ) );
    
	add_submenu_page( "azurecurve-menu-slug"
			, "azurecurve Submenu"
			, "azurecurve Submenu"
			, 0
			, "azurecurve-submenu-slug"
			, "azurecurveSubmenuPageFunction" );
}

The structure of the add_menu_page function is as follows:

add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url);
  • $page_title – is the text to be displayed in the title tags of the page when the menu is selected.
  • $menu_title – the text to be used for the menu.
  • $capability – the capability required for this menu to be displayed to the user; for a settings page to be displayed to users who will maintain settings, this should be set to manage_options.
  • $menu_slug – the slug name to refer to this menu by (should be unique for this menu).
  • $function – the function to be called to output the content for this page.
  • $icon_url – the icon to be displayed next to the top level menu item.

There is another parameter not mentioned above:

  • $position – the position in the menu order this menu should appear which, by default when this parameter is omitted, is that the menu will appear at the bottom of the menu structure. The higher the number, the lower its position in the menu; one word of caution, if two menu items use the same position attribute, one of the items may be overwritten so that only one item displays. The risk of conflict can be reduced by using decimal instead of integer values, e.g. 63.3 instead of 63.

And the structure of the add_submenu_page function is as follows:

add_submenu_page( $parent_slug, page_title, $menu_title, $capability, $menu_slug, $function);
  • $parent_slug – is the slug of the parent menu option.
  • $page_title – is the text to be displayed in the title tags of the page when the menu is selected.
  • $menu_title – the text to be used for the menu.
  • $capability – the capability required for this menu to be displayed to the user; for a settings page to be displayed to users who will maintain settings, this should be set to manage_options.
  • $menu_slug – the slug name to refer to this menu by (should be unique for this menu).
  • $function – the function to be called to output the content for this page.

Click to show/hide the Anatomy Of A WordPress Plugin Series Index

Leave a Reply

Your email address will not be published. Required fields are marked *