Customize the sidebar top menu of the background
First, let us take a look at what is the sidebar of the background Sidebar menu:
comment in the sidebar The menu is not needed. If business users see it, they will be confused. So the best way is to delete the sidebar management menu that should not be there according to the actual situation. The specific implementation method is to add the following code after the first
function remove_menus() { global $menu; $restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins')); end ($menu); while (prev($menu)){ $value = explode(' ',$menu[key($menu)][0]); if(strpos($value[0], '<') === FALSE) { if(in_array($value[0] != NULL ? $value[0]:"" , $restricted)){ unset($menu[key($menu)]); } } else { $value2 = explode('<', $value[0]); if(in_array($value2[0] != NULL ? $value2[0]:"" , $restricted)){ unset($menu[key($menu)]); } } } } if ( is_admin() ) { // 删除左侧菜单 add_action('admin_menu', 'remove_menus'); }
Customize the top-level menu to be removed
The core part of the above code is a function remove_menus(), and then the remove_menus function is executed through the WordPress action interface function add_action. In the remove_menus function, the $restricted array is used to define which menu items need to be deleted. The above $restricted array provides all menu items. That is to say, if you copy all the above codes to functions.php without modification, then your WordPress There will be no menu in the background. You should remove the menu according to actual needs. Let’s talk about the menu corresponding to each $restricted array item:- __('Dashboard'): Control panel menu
- __('Posts') : Article
- __('Media') : Media
- __('Links') : Link
- __('Pages') : Page
- __('Comments') : Comment
- __('Appearance') : Appearance
- __('Plugins') : Plug-in
- __('Users') : User
- __('Tools') : Tool
- __('Settings') : Setting
Comments and Tools menu, you only need to rewrite the $restricted array in line 3 of the above code:
$restricted = array(__('Comments'), __('Tools'));
Delete unnecessary The submenu
There are submenus under the top menu of WordPress backend. Of course, some submenus are not used. We can also delete them. WordPress 3.1 and later versions only need one. Functionremove_submenu_page( $menu_slug, $submenu_slug ), there are two parameters here, $menu_slug is the abbreviation of the top-level menu where the submenu is located, $submenu_slug is the abbreviation of the submenu , then how to obtain these two abbreviations? For example, click on the top-level menuSettings, the link address is similar: http://example/wp-admin/options-general.php
Settings## The abbreviation of # is options-general.php, which is what is left after removing http://example/wp-admin/, and so on; then click on the top menuSettingsThe submenu belowPrivacy, the following URL will be opened: http://example/wp-admin/options-privacy.php
Then the abbreviation of the submenu
For options-privacy.php, okay, it’s that simple. Here is the implementation code:
function remove_submenu() { // 删除"设置"下面的子菜单"隐私" remove_submenu_page( 'options-general.php', 'options-privacy.php' ); // 删除"外观"下面的子菜单"编辑" remove_submenu_page( 'themes.php', 'theme-editor.php' ); } if ( is_admin() ) { add_action('admin_init','remove_submenu'); }Remove the menu based on user role
If you want to remove the corresponding sidebar menu by user role, then just make another judgment on the user level. Add a judgment to add_action on line 11 and rewrite it as:
function remove_menus() { global $menu; // 这里$restricted设置了评论和工具菜单 $restricted = array(__('Comments'), __('Tools')); end ($menu); while (prev($menu)){ $value = explode(' ',$menu[key($menu)][0]); if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);} } } function remove_submenu() { remove_submenu_page( 'options-general.php', 'options-privacy.php' ); } global $current_user; get_currentuserinfo(); //如果当前用户的等级小于3,那么就删除对应的菜单 if ($current_user->user_level < 3 && is_admin()) { add_action('admin_menu', 'remove_menus'); add_action('admin_init','remove_submenu'); }
In the WordPress backend – users, administrators can view/edit user roles. The following is the corresponding relationship between user roles and their levels:
- Level 0 corresponds to Subscribers
- Level 1 corresponds to Contributor
- 2 – Level 4 corresponds to Author
- 5 – Level 7 corresponds to Edit
- 8 – Level 10 corresponds to Administrator
After WordPress 3.0, user numerical levels will be gradually abandoned. It is recommended to use user permissions. You can use Function current_user_can() to determine user permissions.
Use a super simple plug-in
The following recommends a super simple plug-in that does not require any coding. You can achieve the above mentioned results by just dragging the mouse. Some functions. The plug-in name is: Admin Menu Editor, you can click here to go to WordPress official download, after enabling it, go to Settings - Menu Editor, you can edit the background menu, and you can also add external link menus , you can adjust the menu order, etc. You can experience the rest by yourself!