3

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

jDelforge
  • 129
  • 4
  • 15

2 Answers2

3

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']}
Pierre
  • 789
  • 1
  • 4
  • 15
  • Waw. Grazie Pierre ! I'm also using the pro version. – jDelforge Jun 09 '16 at 09:16
  • Actually, it is not -yet- working... If we focus on the wysiwyg case, to turn off the Media Button, `$field['media_upload']` must be set to zero. If i do it in your snippet it does not work. I went to see in wysiwyg.php , and turning it to zero does the job. But the goal is working hack without touching original files.... i have tested different tweaks with no success... I'm not a php/wp filters/actions hero thou. – jDelforge Jun 09 '16 at 10:08
  • 1
    An important point : this snippets will do the job for the display (of the value for the front, and on the field for the back), but they will not check/select the correct option on the ACF fields settings form. – Pierre Jun 09 '16 at 10:26
  • Ha ! Do you think another way of approaching it is possible ? – jDelforge Jun 09 '16 at 10:30
  • 1
    Yep, it is even easier! See my new answer. – Pierre Jun 09 '16 at 10:40
1

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.

Pierre
  • 789
  • 1
  • 4
  • 15
  • 1
    Great ! While it is not possible to change default settings without saving the field... this is the best possible solution... or we need to hack the original files. Anyway... it will work for me. Bedank Pierre ! – jDelforge Jun 10 '16 at 05:39
  • I have posted the solution on the ACF forums. You went futher ;). https://support.advancedcustomfields.com/forums/topic/how-to-change-default-settings-of-fields/ – jDelforge Jun 10 '16 at 05:46