Home › Forums › General Issues › Setting ACF true/false field value automatically › Reply To: Setting ACF true/false field value automatically
Create an acf/prepare_field filter
You will need the field key of this filter. I don’t have all the code
<?php
add_filter('acf/prepare_field/key=FIELD_KEY_HERE', 'FUNCTION_NAME_HERE');
function FUNCTION_NAME_HERE($field) {
if (is_admin()) {
return $field;
}
// get the current user level/role and check
if ($role_can_create_featured) {
// if the user is able to create a featured post
// set the value default value to tru
$field['default_value'] = 1;
return $field;
} else{
// user not allowed to create featured post
// create a hidden field that matches the acf true false field
?><input type="hidden" name="acf[FIELD_KEY_HERE]" value="0" /><?php
// remove the existing field
return false;
}
}
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.