Last updated on September 25, 2023

ACF add_field_group action not adding groups

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Managing Field Groups with add_field_group Action.

The add_action function in WordPress allows you to hook into specific actions that occur during the execution of a WordPress page. One common use case is to add custom fields to a post or page using the Advanced Custom Fields (ACF) plugin.

To add a field group using the add_action function, you need to specify the acf/init action and provide a callback function that will be executed when the action is triggered. Inside the callback function, you can use the acf_add_local_field_group function to define your field group.

Here’s an example of how to add a field group using the add_action function:

function wpsnippets_add_custom_field_group() {
    acf_add_local_field_group(array(
        'key' => 'group_123456789',
        'title' => 'Custom Field Group',
        'fields' => array(
            array(
                'key' => 'field_123456789',
                'label' => 'Custom Field',
                'name' => 'custom_field',
                'type' => 'text',
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'post',
                ),
            ),
        ),
    ));
}
add_action('acf/init', 'wpsnippets_add_custom_field_group');

In this example, we define a callback function named wpsnippets_add_custom_field_group that uses the acf_add_local_field_group function to add a field group. The field group has a unique key, a title, and a single field of type text. We also specify the location where this field group should be displayed, in this case, for posts only.

By adding this code to your theme’s functions.php file or a custom plugin, the field group will be added when the acf/init action is triggered, making the custom field available for posts.

This code snippet can be useful when you want to programmatically add field groups to your WordPress site using ACF. It allows you to define and manage custom fields without relying on the ACF interface.

Examples

Example 1: Adding a field group using the acf/init action hook

This example demonstrates how to add a field group using the acf/init action hook. The acf/init action hook is triggered when ACF initializes, allowing you to add field groups programmatically.

function wpsnippets_add_field_group() {
    if( function_exists('acf_add_local_field_group') ) {
        acf_add_local_field_group(array(
            'key' => 'group_123456789',
            'title' => 'My Field Group',
            'fields' => array(
                // Add your fields here
            ),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'post',
                    ),
                ),
            ),
        ));
    }
}
add_action('acf/init', 'wpsnippets_add_field_group');

In this code example, we define a custom function wpsnippets_add_field_group that uses the acf_add_local_field_group function to add a new field group. The acf_add_local_field_group function accepts an array of parameters, including the field group key, title, fields, and location. We then hook this function to the acf/init action using the add_action function.

Example 2: Adding a field group using the acf/include_fields filter

This example demonstrates how to add a field group using the acf/include_fields filter. The acf/include_fields filter allows you to modify the list of field groups that will be included when ACF initializes.

function wpsnippets_add_field_group( $field_groups ) {
    $field_groups[] = array(
        'key' => 'group_123456789',
        'title' => 'My Field Group',
        'fields' => array(
            // Add your fields here
        ),
        'location' => array(
            array(
                array(
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'post',
                ),
            ),
        ),
    );

    return $field_groups;
}
add_filter('acf/include_fields', 'wpsnippets_add_field_group');

In this code example, we define a custom function wpsnippets_add_field_group that accepts the $field_groups parameter. We add our field group to the $field_groups array and return the modified array. By hooking this function to the acf/include_fields filter using the add_filter function, we can add our field group to the list of included field groups.

Example 3: Adding a field group using the acf/register_fields filter

This example demonstrates how to add a field group using the acf/register_fields filter. The acf/register_fields filter allows you to modify the list of field groups that will be registered when ACF initializes.

function wpsnippets_add_field_group( $field_groups ) {
    $field_groups[] = array(
        'key' => 'group_123456789',
        'title' => 'My Field Group',
        'fields' => array(
            // Add your fields here
        ),
        'location' => array(
            array(
                array(
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'post',
                ),
            ),
        ),
    );

    return $field_groups;
}
add_filter('acf/register_fields', 'wpsnippets_add_field_group');

In this code example, we define a custom function wpsnippets_add_field_group that accepts the $field_groups parameter. We add our field group to the $field_groups array and return the modified array. By hooking this function to the acf/register_fields filter using the add_filter function, we can add our field group to the list of registered field groups.

Last updated on September 25, 2023. Originally posted on October 12, 2023.

Leave a Reply