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

Adding a Menu Item to an Existing Menu

Оглавление

If your plugin requires only a single options page, you may not need to create a custom top‐level menu. Instead, you can simply add a submenu to an existing menu, such as the Settings menu.

WordPress features many different functions to add submenus to the existing default menus in WordPress. One of these functions is the add_options_page() function. Now explore how the add_options_page() function works to add a submenu item to the Settings menu.

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

The add_options_page() function accepts the following parameters:

 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 add a submenu item to the Settings menu.

<?php add_action( 'admin_menu', 'pdev_create_submenu' ); function pdev_create_submenu() { //create a submenu under Settings add_options_page( 'PDEV Plugin Settings', 'PDEV Settings', 'manage_options', 'pdev_plugin', 'pdev_plugin_option_page' ); }?>

The preceding code adds a submenu labeled PDEV Settings under the Settings menu, as shown in Figure 3‐3. Set the page title to PDEV Plugin Settings, set the capability to manage_options so that only administrators can view it, and set the function pdev_plugin_option_page() to be called when the submenu is clicked.


FIGURE 3‐3: Submenu labeled PDEV Settings

The following is a list of all available submenu functions in WordPress:

 add_dashboard_page: Adds a submenu to the Dashboard menu

 add_posts_page: Adds a submenu to the Posts menu

 add_media_page: Adds a submenu to the Media menu

 add_links_page: Adds a submenu to the Links menu

 add_pages_page: Adds a submenu to the Pages menu

 add_comments_page: Adds a submenu to the Comments menu

 add_theme_page: Adds a submenu to the Appearance menu

 add_plugins_page: Adds a submenu to the Plugins menu

 add_users_page: Adds a submenu to the Users menu

 add_management_page: Adds a submenu to the Tools menu

 add_options_page: Adds a submenu to the Settings menu

To use any of these functions, simply swap out the function name in the code shown earlier.

NOTE If your plugin requires only a single options page, it's best practice to add it as a submenu to an existing menu. If you require more than one, create a custom top‐level menu.

Professional WordPress Plugin Development

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