Support

Account

Home Forums Backend Issues (wp-admin) Filter all fields in field group?

Solved

Filter all fields in field group?

  • We have a field group with a number of regular text fields. Data will be imported into these fields from an outside source, so the fields are just for viewing the information and not editing.

    I know you can filter a single field to be read-only with this which works fine:

    function my_acf_load_field( $field ) {
    	$field['disabled'] = 1;
    	return $field;
    }
    add_action('acf/load_field/name=field_name', 'my_acf_load_field');

    But, is there any way to filter all of the fields in a field group the same way? If we have 20 fields in a group that need to be read-only, is there any way to set them all as read-only that doesn’t involve 20 lines of add_action hooks?

    Thanks!

  • You may be able to use the field parent and check for the group, but I’m not sure

    I would also use prepare_field, not load_field

    
    function my_acf_load_field( $field ) {
      if ($field['parent'] == 'group_XXXXXXXXX') {
    	$field['disabled'] = 1;
       }
    	return $field;
    }
    add_action('acf/prepare_field', 'my_acf_load_field');
    
  • Thanks, John!

    I had to modify this a little bit, but it seems like it does the trick. I found that if I didn’t look for a parent value first, things went a little haywire in different spots – for example, when trying to create a new field group.

    Updating to this seems to be doing the trick in my quick test:

    
    function my_acf_load_field( $field ) {
    
    	// If no parent is found, everything kind of breaks.
    	if ( ! $field['parent'] ) {
    		return $field;
    	}
    
    	// Match only the key of the group we want to change.
    	if ( 'group_XXXXXXXXX' == $field['parent'] ) {
    		$field['disabled'] = 1;
    	}
    
    	return $field;
    }
    add_action( 'acf/prepare_field', 'my_acf_load_field' );
    

    Thanks again for the suggestion!

  • Yes, I forgot about flex fields. Field in a flex field do not have ‘parent’ they have ‘parent_layout’ or some such thing.

    you can still to it in a single if like this

    
    if ( !empty$field['parent'] &&  'group_XXXXXXXXX' == $field['parent'] ) {
    
  • Just dropping a big thank you because this is EXACTLY what I needed to solve.

    Thanks!

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Filter all fields in field group?’ is closed to new replies.