Home › Forums › Backend Issues (wp-admin) › Conditionally registering field on acf/update_value
I have a scenario where I wish to register a field on an existing field group when a different repeater field is updated.
(Simplified) code below:
function my_field_registration( $value, $post_id, $field ) {
if ( $field['name'] == 'my_triggering_field' ) {
if ( $some_condition ) {
$args = array(
'key' => 'field_5c58230b9b9f6',
'label' => 'Field label',
'name' => 'my_new_field_name',
'type' => 'number',
'parent' => 'group_5c3f080211566'
);
acf_add_local_field($args);
}
}
return $value;
}
add_filter('acf/update_value', 'my_field_registration', 10, 3);
However, this doesn’t register the field, I assume because my_field_registration is not running in the acf/init hook.
But how then do I register a field conditionally when another field is updated?
You cannot do this on that hook. In order for a field to be registered it must be run on every page load, in other words the init (or acf/init) hook.
It would also be very difficult to do this on the init hook.
Your best option would be to use an acf/prepare_field filter for this field and not display it if a check, or only display it, based on a check of the other field value https://www.advancedcustomfields.com/resources/acf-prepare_field/
I should clarify that I want the registered field to appear in the admin UI.
Should I be creating the field as an acf-field custom post type in the database?
I assumed this was to show the field in the admin. acf/prepare_field is called just before the field is shown in the admin and can be used to not show a field by returning false. You register the field as normal than you create a filter and in the filter you check the value of the other field and then you return the field or FALSE depending on that check.
Thanks for your suggestion John, but that would require me to know all possible field names in advance (so I could hide/show the correct one conditionally). There are infinite possibilities in my case.
What about using acf_update_field to write the field to the database? From includes/api-field.php:
/*
* acf_update_field
*
* This function will update a field into the DB.
* The returned field will always contain an ID
*
* @type function
* @date 1/10/13
* @since 5.0.0
*
* @param $field (array)
* @return $field (array)
*/
function acf_update_field( $field = false, $specific = false )
I could construct a field object and call this function perhaps?
It turns out you can call acf_update_field with almost the same parameters as acf_add_local_field, except I just had to pass the parent as a post_id. Here’s the (simplified) code that I used:
function get_acf_group_id($group_name){
global $wpdb;
return $wpdb->get_var("
SELECT ID
FROM $wpdb->posts
WHERE post_type='acf-field-group' AND post_excerpt='$group_name';
");
}
function my_field_registration( $value, $post_id, $field ) {
if ( $field['name'] == 'my_triggering_field' ) {
if ( $some_condition ) {
$args = array(
'key' => 'field_' . uniqid();,
'label' => 'Field label',
'name' => 'my_new_field_name',
'type' => 'number',
'parent' => get_acf_group_id('my-field-group-name') // hyphenated!
);
acf_update_field($args);
}
}
return $value;
}
add_filter('acf/update_value', 'my_field_registration', 10, 3);
This creates a field programmatically in the database which is editable in the admin.
Hi,
I am successfully using your function in order to create fields based on dynamic checkboxes present in a form.
Everything works fine expect the fields are not viewable in the backend option’s page until I go to the field group and manually save it. Only then the fields appear on the option’s page.
I am using the Json sync feature. Could this be the reason?
Do you have any idea as to why? Is there a way to programmatically save the group after the fields have been added?
I tried using acf_update_field_group() without success.
Thanks
The topic ‘Conditionally registering field on acf/update_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.