add_management_page()
云策文档标注
概述
add_management_page() 函数用于在 WordPress 后台的“工具”主菜单下添加一个子菜单页面。它基于用户权限控制菜单显示,并需要配合 admin_menu Hook 使用以避免权限错误。
关键要点
- 函数功能:向“工具”菜单添加子菜单页面,是 add_submenu_page() 的封装,指定父菜单为 tools.php。
- 参数说明:必需参数包括页面标题、菜单标题、权限能力、菜单 slug;可选参数为回调函数和位置。
- 返回值:返回页面 hook_suffix 或 false(用户无权限时)。
- 权限控制:需在回调函数中检查用户能力,并使用 admin_menu Hook 避免过早挂接导致的权限错误。
- 版本历史:从 WordPress 1.5.0 引入,5.3.0 添加了 $position 参数。
代码示例
add_management_page( 'Custom Permalinks', 'Custom Permalinks', 'manage_options', 'my-unique-identifier', 'custom_permalinks_options_page' );注意事项
如果遇到“您没有足够权限访问此页面”的 wp_die() 错误,通常是因为 Hook 挂接过早,应使用 admin_menu Hook 来调用此函数。
原文内容
Adds a submenu page to the Tools 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_management_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
}
Skip to note 3 content
David Brumbaugh
Example for adding a tool page :
class MyWPTool { function __construct() { add_action( 'admin_menu', array( $this, 'admin_menu' ) ); } function admin_menu() { $hook = add_management_page( 'My WP Tool Page', 'My WP Tool', 'install_plugins', 'mywptool', array( $this, 'admin_page' ), '' ); add_action( "load-$hook", array( $this, 'admin_page_load' ) ); } function admin_page_load() { // ... } function admin_page() { // ... } }Skip to note 4 content
jakubd
// Example add_action ('admin_menu', function () { add_management_page('Some page title', 'Title in the menu', 'install_plugins', 'some_unique_string', 'my_custom_page_render_function', ''); }); function my_custom_page_render_function() { echo 'This is content of the page'; }