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',
        ));
    		
      }
    • amjad

    • February 17, 2018 at 1:50 pm

    @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!

Viewing 6 posts - 26 through 31 (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.