Home › Forums › General Issues › Default values by option page on first load › Reply To: Default values by option page on first load
With both methods, then I would have to do this for each field?
Yes
Is there a way to make a virtual click by the option page update button?
No
I can not understand this exactly.
$value = get_field('my_options', 'option');
‘my_option’ is whatever field name you are trying to get
$value = ‘my-default-value’;
This is just an example, ‘my-default-value’ is whatever value it is you want to be the default. It could be true, false, a number, it depends on what you want the value to be.
ACF does not automatically create default values when a field group is created and it will not return the default value if a value is not set.
Another way that you can work around this would be to create a filter for each field type, for example, a true/false field might be (see https://www.advancedcustomfields.com/resources/acfload_value/)
/*
please note that this is just an example and you will need to test this
I am not 100% sure as I am writing this what $field['default_value']
for a true/false field will contain. The best way to figure this out
is to go to ACF => Tools and export a field groups an you'll see by
looking at each field array what is in the default value
*/
add_filter('acf/load_value/type=true_false', 'get_default_true_false_value', 20, 3);
function get_default_true_false_value($value, $post_id, $field) {
if ($value === NULL) {
$value = $field['default_value'];
}
return $value;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.