add_plugins_page()
云策文档标注
概述
add_plugins_page() 函数用于在 WordPress 后台的“插件”主菜单下添加一个子菜单页面。它是 add_submenu_page() 的包装函数,简化了向插件菜单添加页面的过程。
关键要点
- 函数用于向“插件”菜单添加子菜单页面,需在 admin_menu Hook 中调用。
- 参数包括页面标题、菜单标题、权限、菜单 slug、回调函数和位置,其中权限控制菜单显示和访问。
- 返回值为页面 hook_suffix 或 false(如果用户无权限),回调函数需自行检查用户权限。
- 内部实现基于 add_submenu_page(),指定 'plugins.php' 作为父菜单 slug。
- 从 WordPress 5.3.0 起支持 $position 参数,用于控制菜单项位置。
代码示例
function wpdocs_plugin_menu() {
add_plugins_page(
__( 'WPDocs Plugin Page', 'textdomain' ),
__( 'WPDocs Plugin', 'textdomain' ),
'read',
'wpdocs-unique-identifier',
'wpdocs_plugin_function'
);
}
add_action( 'admin_menu', 'wpdocs_plugin_menu' );注意事项
回调函数必须检查用户是否具有指定的权限,以确保安全访问。
原文内容
Adds a submenu page to the Plugins main menu.
Description
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.
Parameters
$page_titlestringrequired-
The text to be displayed in the title tags of the page when the menu is selected.
$menu_titlestringrequired-
The text to be used for the menu.
$capabilitystringrequired-
The capability required for this menu to be displayed to the user.
$menu_slugstringrequired-
The slug name to refer to this menu by (should be unique for this menu).
$callbackcallableoptional-
The function to be called to output the content for this page.
$positionintoptional-
The position in the menu order this item should appear.
Default:
null
Source
function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
}
Skip to note 2 content
Codex
Basic Plugin Page Example
Typical usage occurs in a function registered with the
admin_menuhook (see Adding Administration Menus):/** * Add a plugin page. */ function wpdocs_plugin_menu() { add_plugins_page( __( 'WPDocs Plugin Page', 'textdomain' ), __( 'WPDocs Plugin', 'textdomain' ), 'read', 'wpdocs-unique-identifier', 'wpdocs_plugin_function' ); } add_action( 'admin_menu', 'wpdocs_plugin_menu' );