Last updated on September 25, 2023

ACF third-party integration issues

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

Resolving Issues with ACF Third-Party Integrations.

ACF (Advanced Custom Fields) is a popular plugin for adding custom fields to WordPress. Sometimes, you may encounter issues when integrating ACF with third-party plugins or themes. In this case, you can use the acf/save_post hook to perform additional actions or validations when saving a post with ACF fields.

Here’s an example code snippet that demonstrates how to use the acf/save_post hook to perform some custom actions when saving a post:

function wpsnippets_acf_save_post( $post_id ) {
    // Check if ACF fields are present
    if ( function_exists( 'get_field' ) ) {
        // Get the value of a specific ACF field
        $field_value = get_field( 'field_name', $post_id );

        // Perform custom actions based on the field value
        if ( $field_value === 'some_value' ) {
            // Do something
        } elseif ( $field_value === 'another_value' ) {
            // Do something else
        }
    }
}
add_action( 'acf/save_post', 'wpsnippets_acf_save_post', 10, 1 );

In this code snippet, we define a custom function wpsnippets_acf_save_post that accepts the $post_id as a parameter. Within the function, we first check if the get_field function exists, which is a function provided by ACF to retrieve the value of a specific field. This check ensures that the code doesn’t throw errors if ACF is not active or installed.

Next, we retrieve the value of a specific ACF field using the get_field function, passing the field name and the post ID as parameters. You should replace 'field_name' with the actual name of the ACF field you want to retrieve.

After retrieving the field value, you can perform custom actions based on its value. In the example, we use a simple if-elseif condition to check the field value and execute different actions accordingly. You can modify this part to suit your specific needs.

Finally, we use the add_action function to hook our custom function wpsnippets_acf_save_post to the acf/save_post action. This ensures that our function is called whenever a post with ACF fields is saved.

This code snippet can be useful when you need to perform additional actions or validations based on the values of ACF fields when saving a post. It allows you to extend the functionality of ACF and integrate it with other plugins or themes seamlessly.

Examples

Example 1: Integrating ACF with a Custom Plugin

This example demonstrates how to integrate Advanced Custom Fields (ACF) with a custom plugin. The code example shows how to register a new ACF field group and display the field value on the front end.

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

function wpsnippets_display_custom_field() {
    $custom_field = get_field('my_custom_field');
    if( $custom_field ) {
        echo '<p>' . $custom_field . '</p>';
    }
}

The code registers a new ACF field group using the acf_add_local_field_group() function. It includes a single text field named “My Custom Field”. The field group is set to appear only on the “post” post type. The wpsnippets_display_custom_field() function retrieves the value of the custom field using the get_field() function and displays it on the front end.

Example 2: Integrating ACF with a Theme

This example demonstrates how to integrate Advanced Custom Fields (ACF) with a WordPress theme. The code example shows how to register a new ACF field group and display the field value within a template file.

function wpsnippets_register_acf_field_group() {
    if( function_exists('acf_add_local_field_group') ) {
        acf_add_local_field_group(array(
            'key' => 'group_123456789',
            'title' => 'My Custom Field Group',
            'fields' => array(
                array(
                    'key' => 'field_123456789',
                    'label' => 'My Custom Field',
                    'name' => 'my_custom_field',
                    'type' => 'text',
                ),
            ),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'page',
                    ),
                ),
            ),
        ));
    }
}
add_action('acf/init', 'wpsnippets_register_acf_field_group');

function wpsnippets_display_custom_field() {
    $custom_field = get_field('my_custom_field');
    if( $custom_field ) {
        echo '<p>' . $custom_field . '</p>';
    }
}

The code registers a new ACF field group using the acf_add_local_field_group() function. It includes a single text field named “My Custom Field”. The field group is set to appear only on the “page” post type. The wpsnippets_display_custom_field() function retrieves the value of the custom field using the get_field() function and displays it within a template file.

Example 3: Integrating ACF with a Custom Post Type

This example demonstrates how to integrate Advanced Custom Fields (ACF) with a custom post type. The code example shows how to register a new ACF field group and display the field value within the custom post type’s single template.

function wpsnippets_register_acf_field_group() {
    if( function_exists('acf_add_local_field_group') ) {
        acf_add_local_field_group(array(
            'key' => 'group_123456789',
            'title' => 'My Custom Field Group',
            'fields' => array(
                array(
                    'key' => 'field_123456789',
                    'label' => 'My Custom Field',
                    'name' => 'my_custom_field',
                    'type' => 'text',
                ),
            ),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'my_custom_post_type',
                    ),
                ),
            ),
        ));
    }
}
add_action('acf/init', 'wpsnippets_register_acf_field_group');

function wpsnippets_display_custom_field() {
    $custom_field = get_field('my_custom_field');
    if( $custom_field ) {
        echo '<p>' . $custom_field . '</p>';
    }
}

The code registers a new ACF field group using the acf_add_local_field_group() function. It includes a single text field named “My Custom Field”. The field group is set to appear only on a custom post type named “my_custom_post_type”. The wpsnippets_display_custom_field() function retrieves the value of the custom field using the get_field() function and displays it within the custom post type’s single template.

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

Leave a Reply