Home  >  Article  >  CMS Tutorial  >  WordPress enterprise website building series: delete unnecessary sidebar menus in the background

WordPress enterprise website building series: delete unnecessary sidebar menus in the background

PHPz
PHPzforward
2023-03-22 16:54:221118browse

This article is the first article in a series of tutorials on WordPress enterprise website building. It will tell you how to delete unnecessary sidebar menus in the WordPress backend. I hope it will be helpful to everyone.

WordPress enterprise website building series: delete unnecessary sidebar menus in the background

Customize the sidebar top menu of the background

First, let us take a look at what is the sidebar of the background Sidebar menu:

WordPress enterprise website building series: delete unnecessary sidebar menus in the background

## The picture above shows all the menu items in the sidebar that you see after logging in with an administrator account. WordPress provides different roles. Users have defined permissions for using different functions, so users with different roles will see different sidebar menu items in the background. At this time, the demand comes again. Sometimes even as an administrator, some menus are not used. For example, if you use WordPress to build an introductory website for an enterprise, the site does not have a comment function at all, so the

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], &#39;<&#39;) === FALSE) {
      if(in_array($value[0] != NULL ? $value[0]:"" , $restricted)){
        unset($menu[key($menu)]);
      }
    }
    else {
      $value2 = explode(&#39;<&#39;, $value[0]);
      if(in_array($value2[0] != NULL ? $value2[0]:"" , $restricted)){
        unset($menu[key($menu)]);
      }
    }
  }
}

if ( is_admin() ) {
  // 删除左侧菜单
  add_action(&#39;admin_menu&#39;, &#39;remove_menus&#39;);
}

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
For example, you just want to remove

Comments and Tools menu, you only need to rewrite the $restricted array in line 3 of the above code:

$restricted = array(__(&#39;Comments&#39;), __(&#39;Tools&#39;));

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. Function

remove_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

Then the top-level menu

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

Privacy

For options-privacy.php, okay, it’s that simple. Here is the implementation code:

function remove_submenu() {
	// 删除"设置"下面的子菜单"隐私"
	remove_submenu_page( &#39;options-general.php&#39;, &#39;options-privacy.php&#39; );

	// 删除"外观"下面的子菜单"编辑"
	remove_submenu_page( &#39;themes.php&#39;, &#39;theme-editor.php&#39; );
}

if ( is_admin() ) {
    add_action(&#39;admin_init&#39;,&#39;remove_submenu&#39;);
}

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(__(&#39;Comments&#39;), __(&#39;Tools&#39;));
    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!

Recommended study: "WordPress Tutorial"

The above is the detailed content of WordPress enterprise website building series: delete unnecessary sidebar menus in the background. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:ludou.org. If there is any infringement, please contact admin@php.cn delete