add_comments_page()
云策文档标注
概述
add_comments_page() 函数用于在 WordPress 后台的评论主菜单下添加一个子菜单页面。它基于 add_submenu_page() 实现,需要指定页面标题、菜单标题、权限等参数。
关键要点
- 函数功能:向评论菜单添加子菜单页面,用于扩展后台管理功能。
- 权限控制:通过 $capability 参数控制菜单显示,处理函数需验证用户权限。
- 参数说明:包括 $page_title、$menu_title、$capability、$menu_slug、$callback(可选)、$position(可选)。
- 返回值:返回页面的 hook_suffix 或 false(权限不足时)。
- 版本历史:自 2.7.0 引入,5.3.0 添加 $position 参数。
代码示例
add_action('admin_menu', 'wpdocs_my_plugin_menu');
function wpdocs_my_plugin_menu() {
add_comments_page( __( 'My Plugin Comments', 'textdomain' ), __( 'My Plugin Comments', 'textdomain' ), 'read', 'my-unique-identifier', 'my_plugin_function' );
}
原文内容
Adds a submenu page to the Comments 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_comments_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
}
Skip to note 2 content
Codex
Add admin page under Comments menu
add_action('admin_menu', 'wpdocs_my_plugin_menu'); function wpdocs_my_plugin_menu() { add_comments_page( __( 'My Plugin Comments', 'textdomain' ), __( 'My Plugin Comments', 'textdomain' ), 'read', 'my-unique-identifier', 'my_plugin_function' ); }