Home › Forums › Feature Requests › Dynamically Set ACF Field Default Value
I needed to pass a variable dynamically to set the default for a field in a front-end form. I ended up using a PHP Closure in the add_filter call. I realize that you could use a class/object but this seems cleaner.
Here is more info for those that are interested: PHP Closures, hooks, and WordPress
Do you think this would be helpful to add to the documentation? I know that you have the post on setting the default values for selects, but this would allow people to dynamically set the default value for any field dynamically.
$member_id = $view->member_id; // variable in script to be passed coming from object (could be any variable.)
add_filter('acf/load_field/name=visit_name',
function($field) use ($member_id) {
// the variable after 'use' ($member_id) indicates that it is the one to 'use' from the main script. $field is coming from 'acf/load_field'.
$field['default_value'] = $member_id;
return $field;
}
);
Thanks,
Andy
Hi @CommandZ
Thanks for sharing your code, but I think this is a bit advanced to make it onto the ACF docs. It may confuse many new comers to WP which find the basics a handful.
Thanks
E
You’re right Elliot. ACF is an abstraction layer, an easy and fast one at that :), and most will not need to do the above. Thanks for putting this together. Can’t wait to see your paid version!
@commandz – Any chance you’re still checking this? (Or anyone else who understands it?) I am trying to do this very thing, but I need to populate the default value with the value of another ACF field from a specific page.
Here’s how I tried to implement it in functions.php, but it’s not working:
$address = get_field('center_address', 342);
add_filter('acf/load_field/name=contact_info',
function($field) use ($address) {
$field['default_value'] = $address;
return $field;
}
);
I’m trying to populate the value of field “contact_info” with the value from “center_address”, from post 342. I’m trying to do this on a backend form on the post editing screen, not a frontend form.
I’m using ACF Pro. The center_address and contact_person field are text fields.
THANK YOU!
Nevermind. I answered my own question. I needed to get rid of “use” and format it like so:
function my_acf_load_field( $field ) {
$address = get_field('center_address', 342);
$field['default_value'] = $address;
return $field;
}
add_filter('acf/load_field/name=contact_person', 'my_acf_load_field');
THANK YOU!
Exactly what I needed, fantastic!
Also, if you want to make that field be uneditable/disabled add this (before the return):
$field['disabled'] = 1;
This is great, thanks for sharing.
If there is no field, such as not logged in so can’t get first name, at the moment it displays 0 in the input, how can I set this to be empty/blank if no field?
The topic ‘Dynamically Set ACF Field Default Value’ is closed to new replies.
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.