search
HomeCMS TutorialWordPressWordPress enterprise website building series: delete unnecessary sidebar menus in the background

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:露兜即刻. If there is any infringement, please contact admin@php.cn delete
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!