Support

Account

Home Forums ACF PRO Adding locations to a field group

Solved

Adding locations to a field group

  • Hello,

    I created a plugin based on flexible fields. Once activated a page builder is provided for pages and posts. the source code is available on github

    As long as you just use posts and pages, everything is fine, but today I would like to use my plugin on a website with 2 custom post type, an provide the builder on the edit screens of the CPT as well.

    So my question is : is there a way to add locations to my plugin field group? For example by adding some code in the functions.php file of the theme.
    I checked the documentation about custom locations but it seems it’s not useful there…

    Thanks!

  • You need to add a filter, the hook is undocumented.

    
    add_filter('acf/get_field_group', 'my_modify_field_group_function');
    function my_modify_field_group_function($group) {
      // $group contains all field group settinngs
      // you can modify them and return them
      return $group;
    }
    
  • Hello John,
    Thanks for your answer, I think a filter is what I need but I’m not sure how to modify $group
    I think I have to add this (my 2 CPT) :

    	'location' => array (
    		array (
    			array (
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => 'portfolio',
    			),
    		),
    		array (
    			array (
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => 'team',
    			),
    		),
    	)
    

    But I don’t know how 🙁

  • The location rules are a nested set of arrays, this is your example with some comments

    
    'location' => array (
      // each array added at this lever has a relation of 'OR'
      // in this case the location rules are 
      // post_type == portfolio OR post_type == team
      array (
        // each array at this nesting level has a relation of 'AND'
        array (
          'param' => 'post_type',
          'operator' => '==',
          'value' => 'portfolio',
        ),
        // if this rule had an 'AND' it would appear here
      ),
      // OR
      array (
        array (
          'param' => 'post_type',
          'operator' => '==',
          'value' => 'team',
        ),
      ),
    )
    
  • ok thanks!
    but I can’t use it that way:

    
    add_filter('acf/get_field_group', 'my_modify_field_group_function');
    function my_modify_field_group_function($group) {
    
    	'key' => 'group_58c6bfd3a0239',
            'location' => array (
    		array (
    			array (
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => 'portfolio',
    			),
    		),
    		array (
    			array (
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => 'team',
    			),
    		),
    	)
      return $group;
    }
    

    I think I have to add the field group ID?

  • When ACF calls your filter it sends all of the field group information. You need to alter that information. This means that you need to add values to that nested array.

    add_filter(‘acf/get_field_group’, ‘my_modify_field_group_function’);
    function my_modify_field_group_function($group) {
    if ($field[‘key’] != ‘group_58c6bfd3a0239’) {
    // not our field group
    return;
    }
    // add an OR rule to existing location rules for a specific field group
    $group[‘location’][] = array(
    array(
    // your rule here
    ),
    );
    return $group;
    }

  • ok I begin to understand, thanks for your help and patience!
    I tried the following, but then I got no more ACF fields in the edit views nor in the acf admin view !

    
    add_filter('acf/get_field_group', 'my_modify_field_group_function');
    function my_modify_field_group_function($group) {
    
    	if ($field['key'] != 'group_58c6bfd3a0239') {
    	// not our field group
    	return;
    	}
    	
    	// add an OR rule to existing location rules for a specific field group
    	$group['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' => 'portfolio',
    			),
    		),
    		array (
    			array (
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => 'team',
    			),
    		),
    	);
    	return $group;
    }
    
  • ok, I’m almost there 🙂

    I modified the code like this :

    
    add_filter('acf/get_field_group', 'my_modify_field_group_function');
    function my_modify_field_group_function($group) {
    
    	if ($group['key'] != 'group_58c6bfd3a0239') {   // note: I replaced $field with $group
    	// not our field group
    	return;
    	
    	} else {
    
    		// add an OR rule to existing location rules for a specific field group
    		$group['location'] = array(
    			array (
    				array (
    					'param' => 'post_type',
    					'operator' => '==',
    					'value' => 'portfolio',
    				),
    			),
    			array (
    				array (
    					'param' => 'post_type',
    					'operator' => '==',
    					'value' => 'team',
    				),
    			),
    			array (
    				array (
    					'param' => 'post_type',
    					'operator' => '==',
    					'value' => 'page',
    				),
    			),
    			array (
    				array (
    					'param' => 'post_type',
    					'operator' => '==',
    					'value' => 'post',
    				),
    			)
    		);
    		return $group;
    
    		
    	}
    	
    }
    

    Now it’s working, I have the field group of my plugin available for portfolio and team. But the problem is that I cannot manually create other field groups… They appear inactive, and the fields seems not saved!

    the the screenshots without the filter, and with the filter

  • OK John, I solved this out. I added $group to the first return…

    
    if ($group['key'] != 'group_58c6bfd3a0239') {
       // not our field group
       return $group;
    

    Thanks again for your precious help!

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Adding locations to a field group’ is closed to new replies.