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.

Viewing 20 posts - 26 through 45 (of 45 total)

You must be logged in to reply to this topic.