Support

Account

Home Forums General Issues Setting a default for an image field. Reply To: Setting a default for an image field.

  • So I will answer my own question. For image and taxonomy fields, the way to set a default value is programmatically using add_filter for acf/load_value. In both cases, the filter function returns an ID value, the ID of the image or the ID of the term.

    function gci_acf_load_value_graphic($value, $postid, $field) {
    	$retval = $value;
    	// We only do this for fields named "graphic".
    	if ($field['_name'] != 'graphic') {
    		return($retval);
    	}
    
    	$flds = ['fb1_graphic', 'fb2_graphic', 'fb3_graphic', 'fb4_graphic'];
    	if (in_array($field['name'], $flds)) {
    		if (!$value) {
    			$img = gci_acf_get_default_graphic($field['name']);
    			$retval = $img['ID'];
    		}
    	}
    	return($retval);
    }
    add_filter('acf/load_value/type=image', 'gci_acf_load_value_graphic', 11, 3);