3

Check the screenshot below; all I want to do is to hide certain ACF fields for custom users in the wordpress backend.

enter image description here

Aaron
  • 167
  • 1
  • 9

2 Answers2

7

As of ACF 5.0.0 there is an easier way to do this without having to output CSS. If you use the acf/prepare_field hook and return false the field will not render.

<?php
function so37111468_hide_field( $field ) {

  // hide the field if the current user is not able to save options within the admin
  if ( ! current_user_can( 'manage_options' ) ) {
    return false;
  }

  return $field;
}

add_filter( 'acf/prepare_field/key=MYFIELDKEY', 'so37111468_hide_field' );
?>

The documentation for that filter can be found here: https://www.advancedcustomfields.com/resources/acf-prepare_field/

hereswhatidid
  • 738
  • 9
  • 21
4

If you mean to hide it with CSS, then you should insert custom CSS to admin footer area. For example, you can add such kind of code to your theme's functions.php file:

add_action('admin_footer', 'my_admin_hide_cf');
function my_admin_hide_cf() {
    $u=wp_get_current_user();
    $user_roles = $u->roles;
    if ($user_roles[0]=='CUSTOM_USER_ROLE_NAME'){
    echo '
   <style>
   #acf-FIELD_SLUG_HERE {display:none}
   </style>';
 }
}

And of course you should replace FIELD_SLUG_HERE and CUSTOM_USER_ROLE_NAME values with correct ones. F.e. #acf-FIELD_SLUG_HERE can be #acf-url, CUSTOM_USER_ROLE_NAME can be "contributor".

Elvin Haci
  • 3,503
  • 1
  • 17
  • 22
  • Was busying working on some stuff. Hope it is not too late to say thank you so much.... @Elvin Haci – Aaron Apr 27 '18 at 20:05