Home › Forums › Backend Issues (wp-admin) › Overwrite acf_add_local_field_group
Hi, I have a theme which has all my field groups in and this is used on many websites, but on one in particular I have a child theme and the client wants an additional image field adding to a group – is there a way of overwriting the function in the child themes functions.php or am I going to have to copy the parent file in to the child theme and amend it there?
I would much prefer using functions.php if possible.
Thanks.
Sorry John, it is PHP, in the parent theme there’s a file which is /acf-fields/team.php and in there is:
add_action( 'acf/include_fields', function() {
if ( ! function_exists( 'acf_add_local_field_group' ) ) {
return;
}
acf_add_local_field_group( array(
'key' => 'group_652407356c71f',
'title' => 'Practice Team',
'fields' => array(
-- ALL THE FIELDS HERE --
),
'location' => array(
array(
array(
'param' => 'page',
'operator' => '==',
'value' => '247',
),
),
),
'menu_order' => 0,
'position' => 'acf_after_title',
'style' => 'seamless',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => array(
0 => 'the_content',
),
'active' => true,
'description' => '',
'show_in_rest' => 0,
) );
} );
So this is on many sites but on this 1 particular site I need to be able to just add an additional image field to one of my repeaters, so was hoping to overwrite this function in the child themes functions.php without needing to copy the file.
Thanks.
If it were me I would alter the function you posted to set up the field group args, then add a call to a filter to allow modification in the child themes functions.php file and then call acf_add_local_field_group();
function () {
$args = array(/*field group settings*/);
apply_filters('my-theme-name-my-field-group-name', $args);
acf_add_local_field_group($args);
}
then in the child them you can add
add_filter('my-theme-name-my-field-group-name', 'child_theme_function', 10, 1);
function child_theme_function($args) {
// modify args
return $args;
}
i have a very similar setup where i init the acf field groups and fields through a custom plugin with acf/include_fields and need to overwrite a field setting via child-theme. would be nice to see a build in filter in acf/include_fields hook or in acf_add_local_field_group function
my case is simpler. i just want to hide the field group in the post editor for certain conditions. i can hide/disable the fields simply with acf/load_field hook but then i have an empty field group left.
found a workaround in case someone needs similar code… i show/hide field groups based on another acf field value i use in an option page:
add_filter('acf/get_field_group', 'my_change_field_group_priceinfo');
function my_change_field_group_priceinfo($group) {
if($group['title'] != 'Preisinfo') return $group;
if(!get_field("priceinfo","options)){
require_once ABSPATH . 'wp-admin/includes/template.php';
\remove_meta_box('acf-'.$group["key"], 'post', $group["position"]);
}
return $group;
}
You must be logged in to reply to this topic.
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.