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!

    • jsilver

    • November 8, 2019 at 6:44 am

    @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;
    });
    
    • skafte

    • November 29, 2019 at 3:39 am

    I would also like to add a big +1 to this as a basic feature request 🙂

Viewing 8 posts - 26 through 33 (of 33 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.