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 '); } |