Remove unwanted sidebar menus in the WordPress background

Source: Internet
Author: User

The specific implementation method is to add the following code in functions. php of the current topic:

The code is as follows: Copy code

Function remove_menus (){

Global $ menu;

$ Restricted = array (_ ('dashboard'), _ ('posts'), _ ('media '), _ ('link '), _ ('Pages '), _ ('appearance'), _ ('tool'), _ ('users'), _ ('Settings '), _ ('comments'), _ ('ins ins '));

End ($ menu );

While (prev ($ menu )){

$ Value = explode ('', $ menu [key ($ menu)] [0]);

If (in_array ($ value [0]! = NULL? $ Value [0]: "", $ restricted) {unset ($ menu [key ($ menu)]);}

}

}

If (is_admin ()){

// Delete the left menu

Add_action ('admin _ menu ', 'remove _ menus ');

}

Customize the top menu to be removed

The core part of the above code is a function remove_menus (), which then executes the remove_menus function through the WordPress Action Interface function add_action. In the remove_menus function, you can use the $ restricted array to define which menu items need to be deleted. The above $ restricted array provides all menu items, that is, if you copy all the above code to functions without modification. php, there will be no menu in your WordPress background. You should remove the menu based on your actual needs. The following describes the menus corresponding to each array of $ restricted:

_ ('Dashboard '): control panel menu

_ ('Posts'): article

_ ('Media '): Media

_ ('Link'): LINK

_ ('Pages '): page

_ ('Comments '): comment

_ ('Appearance '): Appearance

_ ('Ins ins'): plug-in

_ ('Users'): User

_ ('Tool'): Tool

_ ('Settings'): set

For example, if you only want to remove comments and tool menus, you only need to rewrite the $ restricted array of Line 1 of the code above:

The code is as follows: Copy code

$ Restricted = array (_ ('comments'), _ ('tools '));

Remove menus based on user roles

Wordpress has five roles: administrator editor (edit) author (author) contributor (contributor) subscriber (subscriber)

Example of how to judge a role:

The code is as follows: Copy code

If (! Current_user_can ('admin ')){......} // When not an administrator

Go to the background level-1 menu and level-2 menu examples of the administrator user, and add the following code to the topic functions. php:

The code is as follows: Copy code

/* Not_administrator_remove_menu_page */
Function n_a_remove_menu_page (){
Remove_submenu_page ('edit. Php', 'post-new. Php'); // remove "write an article" under "article"
Remove_menu_page ('tools. Php ');
Remove_menu_page ('edit-comments. Php ');
}
If (! Current_user_can ('admin') & is_admin ()){
Add_action ('admin _ menu ', 'n' _ a_remove_menu_page ');
}

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.