当前浏览:首页 / WordPress函数 / add_management_page()

add_management_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $callback = '', int $position = null )

将子菜单页添加到工具主菜单

addmore...

menu_page 菜单页more...


描述

这个函数接受一个能力(capability),它将被用来确定一个页面是否包括在菜单中。

用于处理页面输出的函数必须检查用户是否具有所需的能力。


参数

$page_title

(string)(必填) 选中菜单时要在页面标题标签中显示的文本。

$menu_title

(string)(必填) 要用于菜单的文本。

$capability

(string)(必填) 向用户显示此菜单所需的能力。

$menu_slug

(string)(必填) 用于引用此菜单的slug名称(对于此菜单应是唯一的)。

$callback

(callable)(可选) 要调用以输出此页面内容的函数。

默认值: ''

$position

(int)(可选) 此项应出现在菜单顺序中的位置。

默认值: null


返回

(string|false) 返回结果页面的hook_suffix,如果用户不具备所需的能力则返回false。


更多信息

例子:

add_management_page( 'Custom Permalinks', 'Custom Permalinks', 'manage_options', 'my-unique-identifier', 'custom_permalinks_options_page' );

说明:

如果您运行时在wp_die()屏幕上显示消息:您没有足够的权限访问此页面。则说明您过早的挂钩了,您应该使用的挂钩是admin_menu



源码

查看源码 官方文档


更新日志

版本描述
5.3.0添加了$position参数。
1.5.0开始引入

使用示例

  • 示例1

    添加工具页的示例:

    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() {
    		// ...
    	}
    
    }
    
  • 示例2
    // 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';
    }