Support

Account

Home Forums Feature Requests Read-Only Field

Solving

Read-Only Field

  • I created a new Fields form and there does not appear to be a way to add form data that is READ ONLY. I don’t want the user to be able to modify the data, it is controlled by back-end code. Seems like a pretty big oversight.

  • I will bring this to the attention of the developer.

  • @dnavarrojr, your user need to see the field?
    Why not just creating a location rule, restricting the visibility only to admin?

  • @augusto
    Yes, my users need to see the data in the field and I need them to NOT change it in any way.

    However, I don’t allow users to access the back end of the site, so I was able to create a form on the front end for adding/editing data and add the additional “read-only” fields in that front end form.

    The addition of a READ ONLY option for every field type is still a critical need, in my opinion. I’ve been playing with other plugins like ACF and it appears to be the only one that doesn’t support a READ ONLY option.

  • I have posted this as a feature request on the github repo. It’s late Sat afternoon in Australia. Probably won’t here anything until Mon, earliest, on whether or not Elliot has any plans for this. Like I said in the other post, I think he might since there is a readonly attribute on most field types, but then again I’m not the developer so I have no real idea what his plans are.

    edit: excuse me, early Sat morning there 😛

    • Elliot

    • July 6, 2015 at 9:47 am

    Hi guys

    Although there is no interface for the text field setting ‘readonly’ / ‘disabled’, these settings do exist in the field type.

    If you are using local JSON feature or PHP register, you can edit the JSON and add in the extra:

    
    'readonly': 1
    

    Thanks
    E

  • In the discussion on GitHub, Jonathon had a suggestion.

    By just utilizing the message field and essentially created a readonly field in the message.

    So, create one field group for admins and one for everyone else. The one for everyone else shows message fields.

    You can even make this message field dynamic and load the value from the admin field using this add on: https://wordpress.org/plugins/acf-enhanced-message-field/

  • Hi!

    Just chiming in on the message-field approach.
    You don’t really need a second plugin to make the message field dynamic. In my use-case I needed it to display some statuses for the admin user so I just went ahead and used the acf/load_value filter to modify the output of the message field on render.
    http://www.advancedcustomfields.com/resources/acfload_value/

    It works very well even now more than a year later.

  • Thanks, I’ll try the Message Field thing with the filter and see how that works.

    I can post this in a separate thread, but it sure would be nice to attach PHP code to a form to keep things nice and neat together.

    • KornDev

    • March 8, 2016 at 12:01 pm

    If you are using local JSON feature or PHP register, you can edit the JSON and add in the extra:
    'readonly': 1

    @elliot, unfortunately, this setting doesn’t stick if you update the field after making this change. Any saves to the ACF group will overwrite all readonly and disabled customizations back to 0.

    It’d really be great to see this feature usable.

    Thanks!

  • I was messing about with something and I added the following to functions.php. I wasn’t really surprised when it worked since the disabled and readonly attributes already exist for all fields, this just makes them editable.

    
      add_action('acf/render_field_settings/type=text', 'add_readonly_and_disabled_to_text_field');
      function add_readonly_and_disabled_to_text_field($field) {
        acf_render_field_setting( $field, array(
          'label'      => __('Read Only?','acf'),
          'instructions'  => '',
          'type'      => 'radio',
          'name'      => 'readonly',
          'choices'    => array(
            1        => __("Yes",'acf'),
            0        => __("No",'acf'),
          ),
          'layout'  =>  'horizontal',
        ));
        acf_render_field_setting( $field, array(
          'label'      => __('Disabled?','acf'),
          'instructions'  => '',
          'type'      => 'radio',
          'name'      => 'disabled',
          'choices'    => array(
            1        => __("Yes",'acf'),
            0        => __("No",'acf'),
          ),
          'layout'  =>  'horizontal',
        ));
      }
    
  • I’m wondering how to make a field read only, to only users of a specific role?

    For instance I have a custom post type that contains fields that should only be edited by administrators, yet viewable by contributors and editors.

    Can I disable a field’s input via php or javascript based on role?

  • add an acf/load_field filter https://www.advancedcustomfields.com/resources/acfload_field/

    In the filter check the current user to see if they are an admin or not. If they are not an admin then set the disabled property of the field to 1

    
    $field['disabled'] = 1;
    return $field;
    
  • Thanks man!

  • I have this working for the most part, all of my fields are disabled except for the start_date & end_date fields, those are the type “Date Picker” and the pi_contact is the type of “Post Object”.

    Any ideas?

    Also would is it possible to pass an array of fields to disable as list, or is using an add_filter required for each?

    if( !current_user_can( 'administrator' ) ):
    function disable_acf_load_field( $field ) {
    
    $field['disabled'] = 1;
    return $field;
    
    }
    add_filter('acf/load_field/name=project_code', 'disable_acf_load_field');
    add_filter('acf/load_field/name=project_type', 'disable_acf_load_field');
    add_filter('acf/load_field/name=project_status', 'disable_acf_load_field');
    add_filter('acf/load_field/name=funding_year', 'disable_acf_load_field');
    add_filter('acf/load_field/name=start_date', 'disable_acf_load_field');
    add_filter('acf/load_field/name=end_date', 'disable_acf_load_field');
    add_filter('acf/load_field/name=state', 'disable_acf_load_field');
    add_filter('acf/load_field/name=project_primary_institution', 'disable_acf_load_field');
    add_filter('acf/load_field/name=pi_contact', 'disable_acf_load_field');
    
    endif;
  • The fields you’re having problems with are jquery and select2 fields and I don’t know how to disable them or if they can be disabled. You might want to start a new topic, it might get more input.

  • Thinking about this, if I had that many fields that I wanted to not allow people to edit I would create 2 different field groups that are displayed based on the current user. In the group for the people that aren’t allowed to edit the fields I’d use this https://wordpress.org/plugins/acf-enhanced-message-field/ to display the values.

  • Thanks, I’ll see if I can re-work this requirement with your suggestions.

    So I should be able to hide the inputs via a group, yet show them via php/the_field using enhanced message field to the restricted role.

  • I know it’s probably not what you were looking for. I looked into the code in ACF. The date picker field and the post object field don’t apply the value for disabled. To do this you’d need to do it with JavaScript and I’m not exactly sure how to go about doing that.

  • I’ve tried that plugin and it doesn’t seem to run/display AFC’s the_field.

    I’ve been playing with trying to use jQuery to disable input, I have it working for the date picker, but no luck on select2.

    <?php // Disable AFC fields based on user role/
    
    if( !current_user_can( 'administrator' ) ):
    function disable_acf_load_field( $field ) {
    
          $field['disabled'] = true;
    
    return $field;
    
    }
    add_filter('acf/load_field/name=project_code', 'disable_acf_load_field');
    add_filter('acf/load_field/name=project_type', 'disable_acf_load_field');
    add_filter('acf/load_field/name=project_status', 'disable_acf_load_field');
    add_filter('acf/load_field/name=funding_year', 'disable_acf_load_field');
    add_filter('acf/load_field/name=start_date', 'disable_acf_load_field');
    add_filter('acf/load_field/name=end_date', 'disable_acf_load_field');
    add_filter('acf/load_field/name=state', 'disable_acf_load_field');
    add_filter('acf/load_field/name=project_primary_institution', 'disable_acf_load_field');
    add_filter('acf/load_field/name=pi_contact', 'disable_acf_load_field');
    
    //Backend jquery
    add_action('admin_print_footer_scripts', 'afc_disable_admin_jquery');
    function afc_disable_admin_jquery(){
    ?>
    
    <script>
    jQuery(function(jQuery) {
      jQuery(document).ready(function(){
    
        jQuery(".hasDatepicker").prop('disabled', 'disabled');
    
      });
    });
    </script>
    <?php
    }
    
    endif;
    
  • This works for disabling Select-2, add a wrapper id or class to the field, then you can isolate via jquery.

    jQuery("#pi_contact .select2-input").prop('disabled', true);
    jQuery("#pi_contact .select2-results").remove();
  • You can also use the acf data-key attribute which is in the wrapper of the element.
    jQuery('[data-key="field_0123456789abc"] ...

    The plugin I mentioned does not automatically add fields, but it does let you run PHP in a message field so that you can build a customized message using other field values.

  • I would like to add a big +1 to this as a basic feature request. I have a scenario in which fields may or may not be changeable based on other state. Affected fields include a select field, a radio field and a text field. Having to code up separate workarounds for each case is a pretty high cost to pay for some pretty straight-forward functionality.

  • About the select field.. If you check the sourcecode and look at the real select-elements taht gets hidden by select2. Is it set as disabled?

    Because later versions of select2 are supposed to pick up on that and disable the select2 created functionality as well (and it can be toggled with JS after initiation).

    I’ve been on Elliot about updating the select2 lib for some time now 🙂

    • skdsam

    • December 30, 2016 at 10:51 am

    Hi,

    Sorry I know this is an old post but wanted to share my solution with you in case it suits better.

    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;
    }
    
Viewing 25 posts - 1 through 25 (of 31 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.