Remove unwanted menu item methods from WordPress Admin Panel

Source: Internet
Author: User
Tags explode prev

We can do this by adding a function function to remove the menu item in the functions.php file.

Add the following code to the functions.php file in the current topic:

  code is as follows copy code

function Remove_menus () {
Global $menu;
//The menu items that need to be removed are listed below
$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 (In_array ($value [0]!= NULL? $value [0]: " ", $restricted)) {
unset ($menu [Key ($menu)]);
}
}

//Remove Sidebar menu
add_action (' Admin_menu ', ' Remove_menus ');

The $restricted array in the above code provides all the menu items, and if you copy the above code to the functions.php file intact, then you don't have any menus in WordPress background, so You should only remove unnecessary menu items according to the actual needs.

The code is as follows Copy Code

$restricted the menu corresponding to each array item in English:
__ (' Dashboard '): Control Panel
__ (' Posts '): article
__ (' media '): Media
__ (' links '): Links
__ (' pages '): page
__ (' Comments '): Comments
__ (' appearance '): Appearance
__ (' Plugins '): Plugin
__ (' users '): User
__ (' Tools '): Tools
__ (' Settings '): setting


If you want to remove the sidebar menu by different user roles, you can add a user-level judgment. We can do this: add a judgment statement to the add_action mentioned in the above code, and change it to read as follows:

  code is as follows copy code

function Remove_menus () {
Global $menu;
The $restricted here sets the appearance and media two menu items
$restricted = Array (__ (' appearance '), __ (' Media ');
End ($menu);
while (prev ($menu)) {
$value = Explode (', $menu [Key ($menu)][0]);
if (In_array ($value [0]!= NULL? $value [0]: "", $restricted)) {
unset ($menu [Key ($menu)]);
}
}
}
Global $current _user;
Get_currentuserinfo ();
If the current user's level is less than 4, delete the corresponding menu item
if ($current _user->user_level < 4) {
Add_action (' Admin_menu ', ' Remove_menus ');
}


Attached: User roles and their level of correspondence are as follows:
0-Level counterpart subscriber
Level 1 Counterpart Contributors
2–4-level corresponding author
5–7 level corresponding Edit
8–10 level corresponding Administrator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.