Last updated on September 25, 2023

ACF acf/update_field_group filter not working

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

Addressing Issues with acf/update_field_group Filter.

The acf/update_field_group filter allows you to modify the field group before it is saved or updated in Advanced Custom Fields (ACF). However, there are cases where this filter may not work as expected. One common reason is that the filter is not being applied due to the timing of when it is hooked.

To ensure that the acf/update_field_group filter works properly, you need to make sure that you hook your function to the filter at the right time. The best practice is to hook your function to the filter using the acf/init action hook.

Here’s an example of how to properly hook your function to the acf/update_field_group filter:

function wpsnippets_modify_field_group($field_group) {
    // Modify the $field_group object here
    // ...

    return $field_group;
}
add_action('acf/init', 'wpsnippets_hook_modify_field_group');
function wpsnippets_hook_modify_field_group() {
    add_filter('acf/update_field_group', 'wpsnippets_modify_field_group');
}

In the example above, we define a function wpsnippets_modify_field_group that takes the $field_group object as a parameter. Inside this function, you can modify the $field_group object as needed. Finally, we hook the wpsnippets_modify_field_group function to the acf/update_field_group filter using the acf/init action hook.

By hooking your function to the acf/init action hook, you ensure that the acf/update_field_group filter is applied at the right time, allowing you to modify the field group before it is saved or updated.

This code snippet can be useful when you need to modify the field group object before it is saved or updated in ACF. For example, you can use this filter to dynamically add or remove fields based on certain conditions, or to modify the properties of existing fields.

Examples

Example #1: Modifying ACF field group settings before saving

This use case demonstrates how to use the acf/update_field_group filter to modify the settings of an ACF field group before it is saved to the database.

add_filter( 'acf/update_field_group', 'wpsnippets_modify_field_group_settings', 10, 1 );
function wpsnippets_modify_field_group_settings( $field_group ) {
    // Modify field group settings here
    $field_group['position'] = 'acf_after_title';
    $field_group['hide_on_screen'] = array( 'the_content' );

    return $field_group;
}

In this code example, we define a custom function wpsnippets_modify_field_group_settings that accepts the $field_group parameter. Within the function, we can modify the $field_group array to change various settings of the ACF field group. In this case, we are changing the position of the field group to be displayed after the title and hiding it on the post editor screen.

Example #2: Conditionally modifying ACF field group settings

This use case demonstrates how to conditionally modify the settings of an ACF field group based on certain conditions using the acf/update_field_group filter.

add_filter( 'acf/update_field_group', 'wpsnippets_conditionally_modify_field_group_settings', 10, 1 );
function wpsnippets_conditionally_modify_field_group_settings( $field_group ) {
    if ( is_page_template( 'custom-template.php' ) ) {
        // Modify field group settings here
        $field_group['position'] = 'acf_after_title';
        $field_group['hide_on_screen'] = array( 'the_content' );
    }

    return $field_group;
}

In this example, we use the is_page_template() function to check if the current page is using a specific template (custom-template.php). If the condition is met, we modify the $field_group array to change the position and hide certain fields on the post editor screen.

Example #3: Adding custom validation to ACF field group

This use case demonstrates how to add custom validation to an ACF field group using the acf/update_field_group filter.

add_filter( 'acf/update_field_group', 'wpsnippets_add_custom_validation', 10, 1 );
function wpsnippets_add_custom_validation( $field_group ) {
    foreach ( $field_group['fields'] as &$field ) {
        if ( $field['type'] === 'text' ) {
            $field['validate'] = 'custom_validation';
        }
    }

    return $field_group;
}

function custom_validation( $valid, $value, $field, $input ) {
    if ( strlen( $value ) < 5 ) {
        $valid = 'Field value must be at least 5 characters long.';
    }

    return $valid;
}

In this code example, we iterate through each field in the $field_group['fields'] array and check if the field type is 'text'. If it is, we add a custom validation rule by setting the 'validate' attribute of the field to 'custom_validation'. We also define the custom_validation() function to perform the actual validation logic. In this case, we check if the field value is less than 5 characters long and return an error message if it fails the validation.

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

Leave a Reply

Your email address will not be published. Required fields are marked *