Читать книгу Professional WordPress Plugin Development - Brad Williams - Страница 73

Adding a Submenu

Оглавление

Now that you have a new top‐level menu created, create some submenus for it, which are menu items listed below your top‐level menu. For example, Settings is a top‐level menu, whereas General, listed below Settings, is a submenu of the Settings menu. To register a submenu, use the add_submenu_page() function.

<?php add_submenu_page( parent_slug, page_title, menu_title, capability, menu_slug, function ); ?>

The add_submenu_page() function accepts the following parameters:

 parent_slug: Slug name for the parent menu ( menu_slug previously defined)

 page_title: Title of the page as shown in the <title> tags

 menu_title: Name of your submenu displayed on the Dashboard

 capability: Minimum capability required to view the submenu

 menu_slug: Slug name to refer to the submenu; should be a unique name

 function: Function to be called to display the page content for the item

Now that you know how submenus are defined, you can add one to your custom top‐level menu.

<?php add_action( 'admin_menu', 'pdev_create_menu' ); function pdev_create_menu() { //create custom top-level menu add_menu_page( 'PDEV Settings Page', 'PDEV Settings', 'manage_options', 'pdev-options', 'pdev_settings_page', 'dashicons-smiley', 99 ); //create submenu items add_submenu_page( 'pdev-options', 'About The PDEV Plugin', 'About', 'manage_options', 'pdev-about', 'pdev_about_page' ); add_submenu_page( 'pdev-options', 'Help With The PDEV Plugin', 'Help', 'manage_options', 'pdev-help', 'pdev_help_page' ); add_submenu_page( 'pdev-options', 'Uninstall The PDEV Plugin', 'Uninstall', 'manage_options', 'pdev-uninstall', 'pdev_uninstall_page' ); } ?>

The preceding code creates three submenus for your custom top‐level menu: About, Help, and Uninstall, as shown in Figure 3‐2. Each of these submenu items links to a different custom function that can contain any code you want to use for that submenu page.


FIGURE 3‐2: Submenus

NOTE Not all plugins will need submenus. For example, a plugin with a single settings page has no need for additional submenus. When creating your new plugin, it's important to determine if submenus will be needed for a good user experience.

Professional WordPress Plugin Development

Подняться наверх