Support

Account

Home Forums Feature Requests Read-Only Field

Solving

Read-Only Field

  • i am late but you can also try something like this (with type):

    function disable_acf_load_field( $field ) {
    	$field['disabled'] = true;
    	return $field;
    }
    add_filter('acf/load_field/type=text', 'disable_acf_load_field');

    or try to inspect $field array name with matches of your fields like:

    function disable_acf_load_field( $field ) {
    	$fields = "project_code","project_type","project_status","funding_year" // add more here
    	if(in_array($field["name"],$fields)) $field['disabled'] = true;
    	return $field;
    }
    add_filter('acf/load_field/type=text', 'disable_acf_load_field');
  • Here is with new stylized True / False checkboxes. Tested and it works.

    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'      => 'true_false',
          'name'      => 'readonly',
    	  'ui'			=> 1,
    	  'class'	  => 'acf-field-object-true-false-ui'
        ));
    	  
        acf_render_field_setting( $field, array(
          'label'      => __('Disabled?','acf'),
          'instructions'  => '',
          'type'      => 'true_false',
          'name'      => 'disabled',
    	  'ui'			=> 1,
    	  'class'	  => 'acf-field-object-true-false-ui',
        ));
    		
      }
  • @hube2 Tried doing this for a radio button field. It did not work. Any suggestions?

  • Disabling and Setting fields to read only does not always work, depending on the field. I think you need to disable a standard radio field, bu I have not tried it in a while.

  • Perhaps not the most elegant way, but hey, it works!

        
    /**
    *    Prevents a field from being updated from the panel in ACF.
    *    Useful for business-critical fields that must only by updated internally, but still need to be visible on the admin panel.
    */
    function prevents_field_from_being_updated_from_the_panel($value, $post_id, $field) {
            $backtraces = debug_backtrace();
            foreach ($backtraces as $backtrace) {
                if (!empty($backtrace['function']) && $backtrace['function'] == 'update_field') {
                    return $value;
                }
            }
            return get_field('field_5b5fcd08f9ecf', $post_id, false); # Change field_{key} to your field key/name
    }
    # If you have the field Key:
    add_filter('acf/update_value/key=YOUR_FIELD_KEY', 'prevents_field_from_being_updated_from_the_panel', 10, 3);
        
    # If you prefer to use field name:
    # add_filter('acf/update_value/name=YOUR_FIELD_NAME', 'prevents_field_from_being_updated_from_the_panel', 10, 3);
  • Was this ever solved and put into core? Don’t want to be mucking around with functions.php for this functionality. What we need is not a ‘message’, but a Relationship field for example to only show as readonly. Thanks!

  • @pkhunter I wanted to created a read only “relationship” field that gets updated bidirectionally from a linked post. This is the solution I am using:

    // render readonly field on relationship settings
    add_action('acf/render_field_settings/type=relationship', function($field) {
      acf_render_field_setting( $field, array(
        'label'     => __('Read Only?','acf'),
        'instructions'  => '',
        'type'      => 'true_false',
        'name'      => 'readonly',
      'ui'		=> 1,
      'class'	=> 'acf-field-object-true-false-ui'
      ));  
    });
    
    // render relationships as links to the edit pages.
    add_filter('acf/prepare_field/type=relationship', function($field) {
      // check to see if this is a read only relationship field
      if (isset($field['readonly']) && $field['readonly']) {
        // the value is an array of post_ids
        $post_ids = $field['value'];
        // render them as a list
        echo "<ul style='margin-left:5px;'>";
        foreach($post_ids as $post_id) {
          $post = get_post($post_id);
          $edit_link = get_edit_post_link($post_id);
          echo "<li><a href='$edit_link'>" . $post->post_title . "</a></li>";
        }
        echo "</ul>";
        // return false to stop default rendering
        return false;
      }
      return $field;
    });
    
  • I would also like to add a big +1 to this as a basic feature request 🙂

  • Just want to reiterate a simple solution that got buried a bit earlier in the thread, which is to export your field group to PHP and incorporate that into your theme or plugin instead of using the GUI.

    Then you can just add disabled => 1 to any field array that supports it.

  • Some of us are using Oxygen Builder in case that matters for the best approach.

    In my use case I have an ID that I auto-generate and I don’t want anyone to change it – front end, backend – even an admin.

    How might I do that?

    Thanks

  • I’ve implemented the very helpful suggestion from @hube2 registering the read only settings to the settings field. Thanks!

    The only issue I have is that when creating posts programmatically the read only settings aren’t applied – this only happens after the first save in the admin screen.

    Is there a way I can force the setting to be applied when the post is created? Or can I manually trigger a save after post creation that would set them correctly?

  • @tomgreeen The suggestion I made should work, it has nothing to do with the content saved in the field. But then that was done years ago.

  • Cheers. You’re right, it was something else in my implementation causing the issue. It still works nicely. Thanks !

  • you can simply disable the form field front end using css

    input[name=username] {
        pointer-events: none;
      }
  • The following code will add a checkbox in text fields and if you check read-only, it will become read-only for all users.

    The same login will apply to Disabled.

    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'      => 'true_false',
    		'name'      => 'readonly',
    	));
    	acf_render_field_setting( $field, array(
    		'label'      => __('Disabled?','acf'),
    		'instructions'  => '',
    		'type'      => 'true_false',
    		'name'      => 'disabled',
    	));
    }
    
    add_filter('acf/prepare_field', 'filter_acf_prepare_field');
    function filter_acf_prepare_field( $field ) {
    	
    	if( !empty($field['readonly']) ){
    		$field['readonly'] = 1;
    	}
    	if( !empty($field['disabled']) ){
    		$field['disabled'] = 1;
    	}
    	return $field;
    }
  • I believe I have found a bug when setting a post_object field to be disabled (setting it to readonly doesn’t work). The end result is that updating a post that uses a disabled post_object field clears out any values stored in that field. Here’s step by step instructions to reproduce:

    1. Create a Post Object field, say in an Options page. Allowing multiple values to be set seems to not matter.
    2. Go to the Options page and add one or more posts to this field, save, and see that repeated saving of Options works fine; the posts set in the field are still there.
    3. Add the acf/load_field filter to your functions.php in order to set the field so that disabled = 1 (I tried readonly instead but that had no effect).
    4. Refresh the page, and see that the post you previously set is still there.
    5. Save the page/options, and on page load, the Post Object field is now blank
  • In creating a front end edit of posts functionality, read only forms are very important.

  • Hi roidnachrisb

    I have the same problem.

    I have radio buttons (which show a status if emails have been sent).

    I set them to disabled, because readonly isn’t possible, with the following code:

    public function order_read_only_fields_radio_button($field)
    {
        $field['disabled'] = array(
            // set each value option you want to disable
            '0',
            '1',
            // etc
        );
    
        return $field;
    }
    

    I just spent hours trying to find out that I can’t retrieve the values (array or whatever – I tried everything) from these disabled radio fields using get_field.

    It seems like only update_field works on these disabled fields.

  • Stumbled on this after some brief discussion with the PublishPress Capabilities Pro folks who suggested it might be possible to do what we need with ACF alone as they don’t currently have this capability…

    What we have: two custom roles, “Club Editor” and “Club Manager”, where editors generally manage a single club’s entries, and the Manager who can edit any of the clubs’ data.

    What we need: for administrators and Club Managers, we need to be able to enter the date of the last check received for renewal of both membership, and insurance, for each of the clubs, and for what calendar year these funds are applicable to.

    However, for the clubs themselves, they need to be able to read (but not edit) the fields, to verify the accuracy and receipt, of when their last check was received and logged, and for what calendar year those funds were applied

    (so, four fields total, two for ‘date’ marking receipt of checks, and two just recording the ‘calendar year’, but view/editable by Club Manager + Administrator, but viewable-but-not-editable by Club Editor)

    If I’ve been following the discussion aright, I should be avoiding using “disabled”, as in that case the field data would not be preserved across saves, but I’m still a little unsure how this should be best implemented in a current release (the enhanced message field plugin hasn’t been updated in 7 years) of ACF

  • Disabled fields are not submitted. In most cases since they are not submitted they will not be updated, but the current value should not be removed.

    Removal, or clearing of a value, or seeing the wrong value can happen under some conditions and these conditions all have to do with repeaters and flexible content fields. Disabled fields should be avoided in this situation.

    Disabled field can have issues with some field types, as described above for radio and select type fields. These fields are still submitted, even when a single option is disabled, so it will be submitted with no value.

    If I needed a field that needed to be available for review but not edit I would use a different field. Like a standard message field. I would have put in place an acf/prepare_field function for both fields. I would remove one of the fields based on the user type of the currently logged in user. If the message field is being shown I would dynamically generate the message to be displayed based on the value of the other field.

    The enhanced message field had it’s place at one time, but that was before ACF added the acf/prepare field hook. There is nothing that the enhanced massage field could do that cannot be accomplished with a standard message field using this hook.

  • Well that answers part of my question anyway

  • what part did that not answer?

  • Basically, it’s not that difficult to understand.

    We differentiate here between deactivated fields for inputs, radio buttons, etc. – this information is extremely important!!! Because there are different behaviors for the different fields.

    my problem was the radio buttons. if they are disabled via $field[‘disabled’], then they simply accept the value that you get by definition in the ACF configuration. No code, can then change their value.

    My solution for the radio buttons is:
    Find the radio button from the group that is not checked and disable it.
    The values are overwritten by a mail class and cannot be set by the user.

    It indicates whether a confirmation email has been sent or not.

    $('[data-name="order_send_mail_on_order_to_admin"]').find(':radio:not(:checked)').prop('disabled', true);

Viewing 25 posts - 26 through 50 (of 52 total)

The topic ‘Read-Only Field’ is closed to new replies.