jDelforge
jDelforge

Reputation: 126

ACFs : How to change default values / settings

Using Advanced Custom Fields,

  1. Say anytime i want an image field, i need it to output an array as default.
  2. Or anytime i use a wysiwyg, i don’t want it to show the media button
  3. Need a textarea ? i want it with no formatting as default.

How can i change these defaults ?

Any plugins or snippet example for functions.php ?

Thank you !!

Johan

Upvotes: 3

Views: 3160

Answers (2)

Pierre
Pierre

Reputation: 847

OK, for force the setting of an ACF field, you should use another hook, named acf/update_field/. It works like those before :

add_filter('acf/update_field/type=wysiwyg', 'my_custom_acf_filter_wysiwyg', 1);
function my_custom_acf_filter_wysiwyg($field){
    $field['media_upload'] = 0;
    return $field;
}

And you can use these other for target specific fields :

acf/update_field/name={$field['name']}
acf/update_field/key={$field['key']}

When the settings will be saved, their will be take your custom value.

Upvotes: 1

Pierre
Pierre

Reputation: 847

I'am using ACF Pro, so my answer could not works for you...

For your front fields, you can use the acf/format_value hook, and change the $field params you want :

Textarea example:

add_filter('acf/format_value/type=textarea', 'my_custom_acf_filter_textarea', 1, 3);
function my_custom_acf_filter_textarea($value, $post_id, $field){
    remove_filter('acf/format_value/type=textarea', 'my_custom_acf_filter_textarea', 1);
    $field['new_lines'] = '';
    $newvalue = acf_format_value($value, $post_id, $field);
    return $newvalue;
}

Image example:

add_filter('acf/format_value/type=image', 'my_custom_acf_filter_image', 1, 3);
function my_custom_acf_filter_image($value, $post_id, $field){
    remove_filter('acf/format_value/type=image', 'my_custom_acf_filter_image', 1);
    $field['return_format'] = 'array';
    $newvalue = acf_format_value($value, $post_id, $field);
    return $newvalue;
}

Note that you can also force the options for specific fields with this hooks :

acf/format_value/name={$field['_name']}
acf/format_value/key={$field['key']}

And for your backend display, use acf/prepare_field :

WYSIWYG example:

add_filter('acf/prepare_field/type=wysiwyg', 'my_custom_acf_filter_wysiwyg', 1);
function my_custom_acf_filter_wysiwyg($field){
    remove_filter('acf/prepare_field/type=wysiwyg', 'my_custom_acf_filter_wysiwyg', 1);
    $field['media_upload'] = 0;
    return $field;
}

For the admin display, you can also target specific fields with :

acf/prepare_field/name={$field['name']}
acf/prepare_field/key={$field['key']}

Upvotes: 3

Related Questions