WordPress函数文档add_menu_page()
在后台管理界面添加菜单 描述 Add a top level menu page. Specifically,…
在后台管理界面添加菜单
描述
Add a top level menu page.
Specifically, creates a new top level menu section in the admin menu sidebar and registers a hook to callback your function for outputting the page content when the linked menu page is requested. Returns the $hookname.
用法
<?php
add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
?>
参数
$page_title
(string) (必填) The text to be displayed in the title tags of the page when the menu is selected
默认值: None
$menu_title
(string) (必填) The on-screen name text for the menu
默认值: None
$capability
(string) (必填) The capability required for this menu to be displayed to the user. User levels are deprecated and should not be used here!
默认值: None
$menu_slug
(string) (必填) The slug name to refer to this menu by (should be unique for this menu). Prior to Version 3.0 this was called the file (or handle) parameter. If the function parameter is omitted, the menu_slug should be the PHP file that handles the display of the menu page content.
默认值: None
$function
(string) (可选) The function that displays the page content for the menu page.
默认值: None. Technically, the function parameter is optional, but if it is not supplied, then WordPress will assume that including the PHP file will generate the administration screen, without calling a function. Most plugin authors choose to put the page-generating code in a function within their main plugin file. In the event that the function parameter is specified, it is possible to use any string for the menu_slug parameter. This allows usage of pages such as ?page=my_super_plugin_page instead of ?page=my-super-plugin/admin-options.php.
The function must be referenced in one of two ways:
- if the function is a member of a class within the plugin it should be referenced as array( $this, ‘function_name’ )
- in all other cases, using the function name itself is sufficient
$icon_url
(string) (可选) The icon for this menu.
默认值: empty string
- If you have a custom image file, you can use the plugin_dir_url( __FILE__ ) function to get the URL of your plugin directory and then add the image filename to it. Icons should be 20 x 20 pixels or smaller.
- (WP 3.8+) If ‘dashicons-…’, a Dashicon is shown from the collection at https://developer.wordpress.org/resource/dashicons/. For example, the default “gear” symbol could be explicitly specified with ‘dashicons-admin-generic’.
- (WP 3.8+) If ‘data:image/svg+xml;base64…’, the specified SVG data image is used as a CSS background.
- If ‘none’ (previously ‘div’), the icon is replaced with an empty div you can style with CSS.
- If ” (default), the “gear” Dashicon is shown (and menu-icon-generic is added to the CSS classes of the link).
$position
(integer) (可选) The position in the menu order this menu should appear. By default, if this parameter is omitted, the menu will appear at the bottom of the menu structure. The higher the number, the lower its position in the menu. WARNING: if two menu items use the same position attribute, one of the items may be overwritten so that only one item displays! Risk of conflict can be reduced by using decimal instead of integer values, e.g. 63.3 instead of 63 (Note: Use quotes in code, IE ‘63.3’).
默认值: bottom of menu structure
- 2 – Dashboard
- 4 – Separator
- 5 – Posts
- 10 – Media
- 15 – Links
- 20 – Pages
- 25 – Comments
- 59 – Separator
- 60 – Appearance
- 65 – Plugins
- 70 – Users
- 75 – Tools
- 80 – Settings
- 99 – Separator
For the Network Admin menu, the values are different:
- 2 – Dashboard
- 4 – Separator
- 5 – Sites
- 10 – Users
- 15 – Themes
- 20 – Plugins
- 25 – Settings
- 30 – Updates
- 99 – Separator
返回值
string
$hookname used internally to track menu page callbacks for outputting the page inside the global $menu array
示例
Add a custom menu item to the WordPress admin menu, for a user with administrator capability:
Method 1:
1
2
3
4
5
6
7
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
add_action( ‘admin_menu’, ‘register_my_custom_menu_page’ );
function register_my_custom_menu_page() {
add_menu_page( ‘custom menu title’, ‘custom menu’, ‘manage_options’, ‘myplugin/myplugin-admin.php’, ”, plugins_url( ‘myplugin/images/icon.png’ ), 6 );
}
|
With this method the page-generating code should be located in myplugin/myplugin-admin.php:
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?php echo “Admin Page Test”; ?>
|
Method 2:
1
2
3
4
5
6
7
8
9
10
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
add_action( ‘admin_menu’, ‘register_my_custom_menu_page’ );
function register_my_custom_menu_page(){
add_menu_page( ‘custom menu title’, ‘custom menu’, ‘manage_options’, ‘custompage’, ‘my_custom_menu_page’, plugins_url( ‘myplugin/images/icon.png’ ), 6 );
}
function my_custom_menu_page(){
echo “Admin Page Test”;
}
|
注意
- If you’re running into the “You do not have sufficient permissions to access this page” error, then you’ve hooked too early. The hook you should use is admin_menu.
- If you only want to move existing admin menu items to different positions, you can use the admin_menu hook to unset menu items from their current positions in the global $menu and $submenu variables (which are arrays), and reset them elsewhere in the array.
- This function takes a ‘capability’ (see Roles and Capabilities) 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.
- If you are using the Settings API to save data, and need the user to be other than the administrator, will need to modify the permissions via the hook option_page_capability_{$option_group}, where $option_group is the same as option_group in register_setting() . Check out the Settings API.
Example allowing an editor to save data:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
// Register settings using the Settings API
function register_my_setting() {
register_setting( ‘my-options-group’, ‘my-option-name’, ‘intval’ );
}
add_action( ‘admin_init’, ‘register_my_setting’ );
// Modify capability
function my_page_capability( $capability ) {
return ‘edit_others_posts’;
}
add_filter( ‘option_page_capability_my-options-group’, ‘my_page_capability’ );
|
源文件
add_menu_page() 函数的代码位于 wp-admin/includes/plugin.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Add a top level menu page
*
* 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.
*
* @global array $menu
* @global array $admin_page_hooks
* @global array $_registered_pages
* @global array $_parent_pages
*
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
* @param string $menu_title The text to be used for the menu
* @param string $capability The capability required for this menu to be displayed to the user.
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
* @param callback $function The function to be called to output the content for this page.
* @param string $icon_url The url to the icon to be used for this menu.
* * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
* This should begin with ‘data:image/svg+xml;base64,’.
* * Pass the name of a Dashicons helper class to use a font icon, e.g. ‘dashicons-chart-pie’.
* * Pass ‘none’ to leave div.wp-menu-image empty so an icon can be added via CSS.
* @param int $position The position in the menu order this one should appear
*
* @return string The resulting page’s hook_suffix
*/
function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = ”, $icon_url = ”, $position = null ) {
global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
$menu_slug = plugin_basename( $menu_slug );
$admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
$hookname = get_plugin_page_hookname( $menu_slug, ” );
if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
add_action( $hookname, $function );
if ( empty($icon_url) ) {
$icon_url = ‘dashicons-admin-generic’;
$icon_class = ‘menu-icon-generic ‘;
} else {
$icon_url = set_url_scheme( $icon_url );
$icon_class = ”;
}
$new_menu = array( $menu_title, $capability, $menu_slug, $page_title, ‘menu-top ‘ . $icon_class . $hookname, $hookname, $icon_url );
if ( null === $position )
$menu[] = $new_menu;
else
$menu[$position] = $new_menu;
$_registered_pages[$hookname] = true;
// No parent as top level
$_parent_pages[$menu_slug] = false;
return $hookname;
}
|
相关
Administration Menus:
add_menu_page(),
remove_menu_page(),
add_submenu_page(),
remove_submenu_page(),
add_dashboard_page(),
add_posts_page(),
add_media_page(),
add_links_page(),
add_pages_page(),
add_comments_page(),
add_theme_page(),
add_plugins_page(),
add_users_page(),
add_management_page(),
add_options_page()
- 原文:http://codex.wordpress.org/Function_Reference/add_menu_page
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!