Function Reference/add submenu page

来自WordPress中文文档
跳转至: 导航搜索

Description

Add a sub menu page.

NOTE: If you're running into the "You do not have sufficient permissions to access this page." message in a wp_die() screen, then you've hooked too early. The hook you should use is admin_menu.

This function takes a capability which will be used to determine whether or not a page is included in the menu.

The function which is hooked in to handle the output of the page must check that the user has the required capability as well.

This function should normally be hooked in with one of the the admin_menu actions depending on the menu where the sub menu is to appear:

admin_menu The normal, or site, administration menu
user_admin_menu The user administration menu
network_admin_menu The network administration menu

Usage

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

Parameters

tt$parent_slug/tt
(string) (required) The slug name for the parent menu (or the file name of a standard WordPress admin page). Use NULL or set to 'options.php' if you want to create a page that doesn't appear in any menu (see example below).
Default: noinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

Examples:
  1. For Dashboard: add_submenu_page( 'index.php', ... ); Also see add_dashboard_page()
  2. For Posts: add_submenu_page( 'edit.php', ... ); Also see Also see add_posts_page()
  3. For Media: add_submenu_page( 'upload.php', ... ); Also see add_media_page()
  4. For Links: add_submenu_page( 'link-manager.php', ... ); Also see add_links_page()
  5. For Pages: add_submenu_page( 'edit.php?post_type=page', ... ); Also see add_pages_page()
  6. For Comments: add_submenu_page( 'edit-comments.php', ... ); Also see add_comments_page()
  7. For Custom Post Types: add_submenu_page( 'edit.php?post_type=your_post_type', ... );
  8. For Appearance: add_submenu_page( 'themes.php', ... ); Also see add_theme_page()
  9. For Plugins: add_submenu_page( 'plugins.php', ... ); Also see add_plugins_page()
  10. For Users: add_submenu_page( 'users.php', ... ); Also see add_users_page()
  11. For Tools: add_submenu_page( 'tools.php', ... ); Also see add_management_page()
  12. For Settings: add_submenu_page( 'options-general.php', ... ); Also see add_options_page()
tt$page_title/tt
(string) (required) The text to be displayed in the title tags of the page when the menu is selected
Default: noinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

tt$menu_title/tt
(string) (required) The text to be used for the menu
Default: noinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

tt$capability/tt
(string) (required) The capability required for this menu to be displayed to the user.
Default: noinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

tt$menu_slug/tt
(string) (required) The slug name to refer to this menu by (should be unique for this menu). If you want to NOT duplicate the parent menu item, you need to set the name of the $menu_slug exactly the same as the parent slug.
Default: noinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

tt$function/tt
(callback) (optional) The function to be called to output the content for this page.
Default: noinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

The function must be referenced in one of two ways:
  1. if the function is a member of a class within the plugin it should be referenced as array( $this, 'function_name' ) if the class is instantiated as an object or array( __CLASS__, 'function_name' ) if its called statically
  2. in all other cases, using the function name itself is sufficient

Return values

string
The resulting page's hook_suffix, or false if the user does not have the capability required.

Notes

  • For $menu_slug please don't use __FILE__ it makes for an ugly URL, and is a minor security nuance.
  • Within the rendering function $function you may want to access parameters you used in add_submenu_page(), such as the $page_title. Typically, these will work:
    • $parent_slug: get_admin_page_parent()
    • $page_title: get_admin_page_title(), or simply global $title
    • $menu_slug: global $plugin_page

Example

add_action('admin_menu', 'register_my_custom_submenu_page');

function register_my_custom_submenu_page() {
	add_submenu_page( 'tools.php', 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback' ); 
}

function my_custom_submenu_page_callback() {
	echo '<h3>My Custom Submenu Page</h3>';

}

To hide your submenu link from a top level menu item to which it belongs you would instead do,

add_action('admin_menu', 'register_my_custom_submenu_page');

function register_my_custom_submenu_page() {
   
add_submenu_page( 
          null   //or 'options.php' 
        , 'My Custom Submenu Page' 
        , 'My Custom Submenu Page'
        , 'manage_options'
        , 'my-custom-submenu-page'
        , 'my_custom_submenu_page_callback'
    );

}

Change Log

Source File

add_submenu_page() is located in onlyincludecodewp-admin/includes/plugin.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Related

模板:Admin Menu Tags

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude