Support

Account

Home Forums Backend Issues (wp-admin) Overwrite acf_add_local_field_group

Solving

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.

  • How are the field groups added? PHP? JSON? something else?

  • 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;
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.