Home › Forums › General Issues › Override acf_add_local_field
Hello,
I’m wondering if there is a way to create a field group with acf_add_local_field() in a parent theme and then override it in a child theme.
what I’m trying to accomplish is changing the “location” array. something like this:
// parent
acf_add_local_field_group(array (
'key' => 'group_55009056525c1',
'title' => 'Layout',
'fields' => array ( ... ),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
),
),
));
// child -- doesn't work, but this is my thinking
acf_add_local_field(array (
'key' => 'group_55009056525c1',
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
),
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'resources',
),
),
),
));
So I’d like to have this “Layout” group be present on all pages and posts as defined by the parent but then I’d like to be able to override that and add it to any custom post types created in the child…sort of like a acf_update_local_field_group instead of acf_add_local_field_group. Is this possible?
Hi @bpdp
Hmm.. If you look at the function acf_add_local_field_group it calls to add_field_group and in there you’ll find:
// don't allow overrides
if( acf_is_local_field_group($field_group['key']) ) {
return;
}
I’m not sure why this is there so I’m subbing in @elliot and perhaps he can explain it better and give you some tips.
Thanks Jonathan!
I actually tried to comment those couple lines out (just to see if it would work) and still had no luck, but looking forward to finding out if there is a solution from elliot.
Thanks again
Hi guys
@bpdp – your second code block is using the function acf_add_local_field
– this should be changed to acf_add_local_field_group
.
The function will not allow overrides to a field group. This is to prevent potential errors when adding 2 field groups with the same key by mistake.
I’m open to the idea of removing this validation, but not sure if it will cause any issues with existing developers.
Looking at the code, you could edit the field group data like so:
acf_local()->groups[ 'group_55009056525c1' ]['location'] = array( ... );
Thanks @elliot
acf_local()->groups[ 'group_55009056525c1' ]['location'] = array( ... );
This snippet worked perfectly! Thanks again for a great product and amazing support!
The topic ‘Override acf_add_local_field’ 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.