Wordpress Array not working for multiple users - Admin menu items

时间:2017-06-15 10:25:13

标签: php arrays wordpress

I'm trying to remove admin menu items for all users except two. I can do it for one user but the problem comes when i try and add a second user inside an array, the code is as follows:

// HIDE ADMIN MENU ITEMS FOR ALL EXCEPT MAIN USER

function remove_menus(){
     $current_user = wp_get_current_user();
     if( in_array( $current_user->user_email, array('shaun@seedcreativity.co.uk','test@seedcreativity.co.uk',)) ){
       remove_menu_page('link-manager.php'); // Links
       remove_menu_page('edit-comments.php'); // Comments
       remove_menu_page('plugins.php'); // Plugins
       remove_menu_page('tools.php'); // Tools
       remove_menu_page('options-general.php'); // Settings
       remove_menu_page('edit.php?post_type=acf-field-group'); // Custom Fields
       remove_menu_page('cptui_manage_post_types'); // Custom Post Types
       remove_menu_page('cnss_social_icon_page'); // Easy Social Icons
       remove_menu_page('recent-tweets'); // Recent Tweets
       remove_menu_page('responsive-menu'); // Responsive Menu
       remove_menu_page('google-fonts'); // Google Fonts
       remove_menu_page('vc-general'); // Visual Composer
    }

}
add_action( 'admin_menu', 'remove_menus' );

I've tried surrounding the array items with "" instead and removing the last , but it doesn't seem to work... Any help would be amazing!

Thanks,

2 个答案:

答案 0 :(得分:1)

Here's your updated code (the conditional was updated because you want to remove for all except these two):

function remove_menus(){
     $current_user = wp_get_current_user();
     if( ! in_array( $current_user->user_email, array('shaun@seedcreativity.co.uk','test@seedcreativity.co.uk')) ){
       remove_menu_page('link-manager.php'); // Links
       remove_menu_page('edit-comments.php'); // Comments
       remove_menu_page('plugins.php'); // Plugins
       remove_menu_page('tools.php'); // Tools
       remove_menu_page('options-general.php'); // Settings
       remove_menu_page('edit.php?post_type=acf-field-group'); // Custom Fields
       remove_menu_page('cptui_manage_post_types'); // Custom Post Types
       remove_menu_page('cnss_social_icon_page'); // Easy Social Icons
       remove_menu_page('recent-tweets'); // Recent Tweets
       remove_menu_page('responsive-menu'); // Responsive Menu
       remove_menu_page('google-fonts'); // Google Fonts
       remove_menu_page('vc-general'); // Visual Composer
    }

}
add_action( 'admin_menu', 'remove_menus' );

答案 1 :(得分:0)

If you want below two user can access the admin pages then try below code :

.....

 if(! in_array( $current_user->user_email, array('shaun@seedcreativity.co.uk','test@seedcreativity.co.uk')) ){
       remove_menu_page('link-manager.php'); // Links
       remove_menu_page('edit-comments.php'); // Comments
       remove_menu_page('plugins.php'); // Plugins

......