文档章节

Function Reference/add submenu page

Amamatthew
 Amamatthew
发布于 2014/09/02 14:24
字数 691
阅读 7
收藏 0

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:


Usage

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

Parameters

  • $parent_slug

  • (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:

  • 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()

    13. For Settings in the Network Admin pages: add_submenu_page( 'settings.php', ... );

  • $page_title

  • (string) (required) The text to be displayed in the title tags of the page when the menu is selected

    • Default:

  • $menu_title

  • (string) (required) The text to be used for the menu

    • Default:

  • $capability

  • (string) (required) The capability required for this menu to be displayed to the user.

    • Default:

  • $menu_slug

  • (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:

  • $function

  • (callback) (optional) The function to be called to output the content for this page.

    • Default:

  • 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 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 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 '<div class="wrap"><div id="icon-tools" class="icon32"></div>';
		echo '<h2>My Custom Submenu Page</h2>';
	echo '</div>';

}

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'
    );

}

If you are attempting to add a sub menu page to a menu page created via add_menu_page() the first sub_menu_page will duplicate your add_menu_page().

If you want a sub_menu_page in this scenario, you should first create a duplicate of your add_menu_page() and then add your sub_menu_page() second:

add_menu_page('My Custom Page', 'My Custom Page', 'manage_options', 'my-top-level-slug');
add_submenu_page( 'my-top-level-slug', 'My Custom Page', 'My Custom Page', 'manage_options', 'my-top-level-slug');
add_submenu_page( 'my-top-level-slug', 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-secondary-slug');

Change Log

Source File

add_submenu_page() is located in wp-admin/includes/plugin.php.


本文转载自:http://codex.wordpress.org/Function_Reference/add_submenu_page

Amamatthew
粉丝 67
博文 1050
码字总数 76443
作品 0
厦门
后端工程师
私信 提问
第一个wordpress 插件的编写

前两天因为毕设答辩和体育长跑请了两天假,才上班就请假感觉不太爽。 我的第一个任务就是为WordPress开发插件然后武装成CMS,作为管理后台。同时肖成兄给我办了企业邮箱,感觉瞬间进了职场有...

devforma
2013/11/22
510
5
WordPress后台添加侧边栏菜单

添加WordPress顶级管理菜单其实也是一件非常简单的事情,使用两个WordPress内置函数就可以解决问题,分别是addmenupage()和addaction(),下面我提供一个php示例代码,添加到主题目录下的fun...

李佳顺
2012/09/12
1K
0
WordPress后台添加侧边栏菜单(WP教程add_menu_page)

本文是WordPress企业建站系列教程的第三篇文章,需要说明的是,如果想你学习这个企业建站的系列教程,需要你有一点点的WordPress主题或PHP开发基础,没有基础的也可以依样画葫芦,出错了可以...

老朱教授
2017/10/09
0
0
WordPress后台删除不需要的侧边栏菜单

自定义后台的侧边栏顶级菜单 首先让我们看看,什么是后台的侧边栏菜单: 上图就是使用管理员账号登陆后看到的侧边栏所有的菜单项,WordPress给不同角色的用户定义了不同功能的使用权限,所以不...

李佳顺
2012/09/13
151
0
SPORTSTORE OPENCART 商城自适应主题模板 ABC-0003-01

SPORTSTORE OPENCART 商城自适应主题模板 ABC-0003-01 Before you upgrade your store be sure to read the READ FIRST!!!.HTML SportStore is responsive, multi-purpose and modern OpenCa......

OpenCart中国
2015/06/28
77
0

没有更多内容

加载失败,请刷新页面

加载更多

Spring Security 自定义登录认证(二)

一、前言 本篇文章将讲述Spring Security自定义登录认证校验用户名、密码,自定义密码加密方式,以及在前后端分离的情况下认证失败或成功处理返回json格式数据 温馨小提示:Spring Security...

郑清
21分钟前
1
0
php yield关键字以及协程的实现

php的yield是在php5.5版本就出来了,而在初级php界却很少有人提起,我就说说个人对php yield的理解 Iterator接口 在php中,除了数组,对象可以被foreach遍历之外,还有另外一种特殊对象,也就是继承...

冻结not
34分钟前
2
0
servlet请求和响应的过程

本文转载于:专业的前端网站➥servlet请求和响应的过程 1.加载 Servlet类被加载到Java虚拟机中,并且实例化。在这个过程中,web容器(例如tomcat)会调用Servlet类的公开无参构造函数,产生一...

前端老手
34分钟前
2
0
golang 1.13 errors 包来了,不用写“err 气功波”代码

引 这篇是对 errors 包 的姿势挖掘 气功波错误代码 从 http.Get()返回的错误 判断 syscall.ECONNREFUSED 错误.以前要对 go 标准库 error 结构有点熟悉,才能写出下面的代码 func CmdErr(err ...

guonaihong
38分钟前
25
0
喜玛拉雅已听书单

时间倒序排 书名 作者 状态 唐砖 孑与2 进行中 死灵之书(克苏鲁神话合集) 阿卜杜拉·阿尔哈萨德 进行中 赡养人类 刘慈欣 完结 赡养上帝 刘慈欣 完结 中国太阳 刘慈欣 完结 中国太阳 刘慈欣...

Alex_Java
39分钟前
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部