Ask your WordPress questions! Pay money and get answers fast! (more info)

Need to hide admin menu item except for certain user

  • SOLVED

I have code that hides the Advanced Custom Fields admin menu item for either all users, or certain users.

Here's the two code sets:

All users:

add_filter('acf/settings/show_admin', '__return_false');

Certain users:

add_filter('acf/settings/show_admin', 'my_acf_show_admin');

function my_acf_show_admin( $show ) {

return current_user_can('manage_options');

}


I want to hide it for all users <strong>except</strong> a certain user called 'oilman.'

Can anyone give me that code? Thanks!

Reference: [[LINK href="http://www.advancedcustomfields.com/resources/how-to-hide-acf-menu-from-clients/"]]http://www.advancedcustomfields.com/resources/how-to-hide-acf-menu-from-clients/[[/LINK]]

Answers (4)

2015-08-21

Andrea P answers:

Hello,

the second function basically returns false if the current user cannot manage options, and true if he can.

so you can build up any logic, which will return false in a case and true in the other.

by instance



function my_acf_show_admin( $show ) {

if ( get_current_user_id() == "10" ){
return true; // show it
}
else {
return false; // hide it
}

}


Kyler Boudreau comments:

Andrea,

So where would I specify the user 'oilman'? Where the "10" is? Thanks!


Andrea P comments:

Hi ya!

the 10 is the id of the user which will be the only one allowed. so if you go to the users list and hover the mouse on oilman name, then you look at the url bar in the browser, and near the end you'll see a query-var with the user id.

put that user id in the place of 10, and that should show the item only to oilman and hide it to anyone else.

cheers!

2015-08-22

timDesain Nanang answers:

just following what @Andrea said,
try the following code:
add_filter('acf/settings/show_admin', 'my_acf_show_admin');
function my_acf_show_admin( $show ) {
$current_user = wp_get_current_user();

if(current_user_can('manage_options') AND $current_user->user_nicename == 'oilman'){
return true;
}
else{
return false;
}
}


Kyler Boudreau comments:

Thanks Tim!

2015-08-21

Balanean Corneliu answers:

You have tryed this code? function remove_menus()
{
global $menu;
global $current_user;
get_currentuserinfo();

if($current_user->user_login != 'admin')
{
$restricted = array(__('Posts'),
__('Media'),
__('Links'),
__('Pages'),
__('Comments'),
__('Appearance'),
__('Plugins'),
__('Users'),
__('Tools'),
__('Settings'),
__('Options')
);
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}// end while

}// end if
}
add_action('admin_menu', 'remove_menus');


Try to change the admin with your specific user

2015-08-21

Kyle answers:

The admin menu editor plugin can handle this https://wordpress.org/plugins/admin-menu-editor/