Support

Account

Home Forums ACF PRO Update Default Value of Field Object

Solved

Update Default Value of Field Object

  • Hi, I’m wondering if there is an API function where I can update the default value for a particular field object.

    The scenario is that a user will choose their stylesheet from an options page, and based on their choice I want to update the color picker field objects default values. I have all of the field keys.

    Any help would be appreciated! Also, this is a spectacular plugin. Thanks!

  • I managed to do it by using the method below, but would like to know if there is a better way to do this.

    function acf_load_red_color($field) {
    	$field['default_value'] = '#EA2830';
    	return $field;
    }
    function acf_load_green_color($field) {
    	$field['default_value'] = '#219BA6';
    	return $field;
    }
    function acf_load_blue_color($field) {
    	$field['default_value'] = '#000000';
    	return $field;
    }
    $style = get_field('base_style', 'option');
    if($style == 'red') {
    	add_filter( 'acf/load_field/key=field_53bd8ce3bfc22', 'acf_load_red_color' );
    	add_filter( 'acf/load_field/key=field_53bbfc499145f', 'acf_load_red_color' );
    } elseif ($style == 'green') {
    	add_filter( 'acf/load_field/key=field_53bd8ce3bfc22', 'acf_load_green_color' );
    	add_filter( 'acf/load_field/key=field_53bbfc499145f', 'acf_load_green_color' );
    } elseif ($style == 'blue') {
    	add_filter( 'acf/load_field/key=field_53bd8ce3bfc22', 'acf_load_blue_color' );
    	add_filter( 'acf/load_field/key=field_53bbfc499145f', 'acf_load_blue_color' );
    }
    
  • Hi guys.

    The above solution is good, and the other way to do it is to update the field’s setting using the acf_update_field function.

    First use acf_get_field(‘field_key’) to load the field, modify the settings and then use acf_update_field($field) to update it back into the DB

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

The topic ‘Update Default Value of Field Object’ is closed to new replies.