Support

Account

Home Forums Backend Issues (wp-admin) Disabled Field doesn't work if it has Conditional Requirements

Helping

Disabled Field doesn't work if it has Conditional Requirements

  • So I know there are a lot of topics regarding the varying degrees of success of making a field disabled / readonly.

    After pulling my hair out trying extensive testing I found something definitive that I haven’t seen touched here.

    If a field you want to mark as disabled with a filter eg:

    
    function acf_notempty_field_disable( $field ) {
    	if( !empty( $field[ 'value' ] ) ) {
    		$field[ 'disabled' ] = true;
    	}
    	return $field;
    }
    add_filter( 'acf/prepare_field/key=field_5d89cbb86100b', 'acf_notempty_field_disable' );
    

    This won’t work if in the Field Group you have given this field a conditional marker. Now I haven’t figured out a workaround but I thought I might share this and see if someone smarter than me has an idea of how to get around this?

  • Hi,

    I know this is a couple of years old, but hopefully this will save someone else some time.

    The work around I wrote uses the ACF filters to make sure disabled fields could not be accidentally overwritten by an admin page that was left open and then saved after the meta has been updated elsewhere in the code. Example:

    function my_acf_update_value( $value, $post_id, $field, $original ) {
    		
    	if($field['disabled']){
    		// if the field is supposed to be disabled, return the live meta value
    		$value = get_post_meta($post_id,$field['name'],true);
    	}
    		
    	return $value;
    }
    
    // Apply to all fields.
    add_filter('acf/update_value', 'my_acf_update_value', 10, 4);

    Now, if you want to update the meta value elsewhere, you will now need to use update_post_meta() as update_field() will use the above filter.

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

The topic ‘Disabled Field doesn't work if it has Conditional Requirements’ is closed to new replies.