WordPress函数文档add_submenu_page()
在后台管理界面添加二级菜单 描述 Add a sub menu page. NOTE: If you’re r…
在后台管理界面添加二级菜单
描述
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 |
用法
<?php add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function ); ?>
参数
$parent_slug
(string) (必填) 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).
默认值:
Examples:
- For Dashboard: add_submenu_page( ‘index.php’, … ); Also see add_dashboard_page()
- For Posts: add_submenu_page( ‘edit.php’, … ); Also see Also see add_posts_page()
- For Media: add_submenu_page( ‘upload.php’, … ); Also see add_media_page()
- For Links: add_submenu_page( ‘link-manager.php’, … ); Also see add_links_page()
- For Pages: add_submenu_page( ‘edit.php?post_type=page’, … ); Also see add_pages_page()
- For Comments: add_submenu_page( ‘edit-comments.php’, … ); Also see add_comments_page()
- For Custom Post Types: add_submenu_page( ‘edit.php?post_type=your_post_type’, … );
- For Appearance: add_submenu_page( ‘themes.php’, … ); Also see add_theme_page()
- For Plugins: add_submenu_page( ‘plugins.php’, … ); Also see add_plugins_page()
- For Users: add_submenu_page( ‘users.php’, … ); Also see add_users_page()
- For Tools: add_submenu_page( ‘tools.php’, … ); Also see add_management_page()
- For Settings: add_submenu_page( ‘options-general.php’, … ); Also see add_options_page()
- For Settings in the Network Admin pages: add_submenu_page( ‘settings.php’, … );
$page_title
(string) (必填) The text to be displayed in the title tags of the page when the menu is selected
默认值:
$menu_title
(string) (必填) The text to be used for the menu
默认值:
$capability
(string) (必填) The capability required for this menu to be displayed to the user.
默认值:
$menu_slug
(string) (必填) 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.
默认值:
$function
(callback) (可选) The function to be called to output the content for this page.
默认值:
The function must be referenced in one of two ways:
- 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
- in all other cases, using the function name itself is sufficient
注意
- For $menu_slug please don’t use __FILE__ it makes for an ugly URL, and is a minor security nuisance.
- 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 simplyglobal $title
- $menu_slug:
global $plugin_page
- $parent_slug:
历史
- 添加于 版本: 1.5.0
源文件
add_submenu_page() 函数的代码位于 wp-admin/includes/plugin.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Add a sub menu page
*
* 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.
*
* @global array $submenu
* @global array $menu
* @global type $_wp_real_parent_file
* @global bool $_wp_submenu_nopriv
* @global array $_registered_pages
* @global array $_parent_pages
*
* @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page)
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
* @param string $menu_title The text to be used for the menu
* @param string $capability The capability required for this menu to be displayed to the user.
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
* @param callback $function The function to be called to output the content for this page.
*
* @return false|string The resulting page’s hook_suffix, or false if the user does not have the capability required.
*/
function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = ” ) {
global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,
$_registered_pages, $_parent_pages;
$menu_slug = plugin_basename( $menu_slug );
$parent_slug = plugin_basename( $parent_slug);
if ( isset( $_wp_real_parent_file[$parent_slug] ) )
$parent_slug = $_wp_real_parent_file[$parent_slug];
if ( !current_user_can( $capability ) ) {
$_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
return false;
}
/*
* If the parent doesn’t already have a submenu, add a link to the parent
* as the first item in the submenu. If the submenu file is the same as the
* parent file someone is trying to link back to the parent manually. In
* this case, don’t automatically add a link back to avoid duplication.
*/
if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
foreach ( (array)$menu as $parent_menu ) {
if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
$submenu[$parent_slug][] = array_slice( $parent_menu, 0, 4 );
}
}
$submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
$hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
if (!empty ( $function ) && !empty ( $hookname ))
add_action( $hookname, $function );
$_registered_pages[$hookname] = true;
/*
* Backward-compatibility for plugins using add_management page.
* See wp-admin/admin.php for redirect from edit.php to tools.php
*/
if ( ‘tools.php’ == $parent_slug )
$_registered_pages[get_plugin_page_hookname( $menu_slug, ‘edit.php’)] = true;
// No parent as top level.
$_parent_pages[$menu_slug] = $parent_slug;
return $hookname;
}
|
相关
Administration Menus:
add_menu_page(),
remove_menu_page(),
add_submenu_page(),
remove_submenu_page(),
add_dashboard_page(),
add_posts_page(),
add_media_page(),
add_links_page(),
add_pages_page(),
add_comments_page(),
add_theme_page(),
add_plugins_page(),
add_users_page(),
add_management_page(),
add_options_page()
- 原文:http://codex.wordpress.org/Function_Reference/add_submenu_page
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!