Support

Account

Home Forums Front-end Issues ACF Hide Field from Role(s) Reply To: ACF Hide Field from Role(s)

  • Hi,

    I have a front end form and have found a solution to setting fields as not editable for different user roles. I have my code in the themes single post and run the below code when I’m getting the posts.

    while ( have_posts() ) : the_post();
    
     if(current_user_can('administrator'))
     {
         // get post id from have post	 
         $post = get_the_ID();
    
        // This is our form on front end and admins role can see and edit all fields
        $new_post = array(
            'post_id'            => $post, // get the post from id above
            'new_post'           => false,
            // PUT IN YOUR OWN FIELD GROUP ID(s)
    	  //'field_groups'       => array(12), // Create post field group ID(s)
            'form'               => true,
    	    'return'             => '%post_url%', // Redirect to post url
            'html_before_fields' => '',
            'html_after_fields'  => '<hr />',
            'uploader'           => 'wp',
            'honeypot'           => true,
            //'fields'             => array('building'),
            'submit_value'       => 'Update activity',
            'updated_message'    => 'Saved!'
        );
    
     }
    //  can be any user role even a custom user role  if(current_user_can(‘editor’))  or everyone else like below
     else
     {
    
         // Display the fields only no editing for set user role
         echo '<hr /><div class="acf-label"><label for="">Building</label></div>';
         echo '<div class="acf-input"><div class="acf-input-wrap">'.get_field('building').' </div>';
         
         // get post id from have post	 
         $post = get_the_ID();
    
        // This is our form on front end
        $new_post = array(
            'post_id'            => $post, // get the post from id above
            'new_post'           => false,
            // PUT IN YOUR OWN FIELD GROUP ID(s)
    	//'field_groups'       => array(12), // Create post field group ID(s)
            'form'               => true,
    	'return'             => '%post_url%', // Redirect to post url
            'html_before_fields' => '',
            'html_after_fields'  => '<hr />',
            'uploader'           => 'wp',
            'honeypot'           => true,
            // Add your fields here **** Note if you have tabs you need to use the field_key for them to display might be worth using all field_keys
            'fields'             => array('field_5859bd81f57e7','activity','start_date','completed_date','status','field_5859be28a1187','task_details', 'field_5859bda0f57e8','comments', 'field_5859be7473422' ,'document'),
            'submit_value'       => 'Update activity',
            'updated_message'    => 'Saved!'
        );
    
     }
     
     
    acf_form( $new_post );
    
    endwhile;

    Some fields can be made to be disabled using the following code but this does not work for select field and other types I even tried to add css injection but that made the tab fields not display and did not work on the user select field how ever I tried to dirty hack.

    //////////////////////////////////////////////////////////////////
    // Render our field to stop non roles from editing
    //////////////////////////////////////////////////////////////////
    add_filter('acf/prepare_field/name=activity', 'my_acf_prepare_field');
    function my_acf_prepare_field( $field ) 
    {
    
     $current_user = wp_get_current_user();
     $cu = $current_user->user_login;
     $value_personcu = get_field( “activity” );
     $select_staff_id = $value_personcu['ID'];
     $rr =  $current_user->ID;
    
     if( current_user_can('administrator') ){}
     else
     {
       // set a text field to non editable
       $field['disabled'] = true; 
     }
      return $field;
    }