Last updated on September 14, 2023

ACF acf/save_post filter and post processing

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

Post-processing with ACF acf/save_post Filter.

The ACF acf/save_post filter is a powerful tool in WordPress development that allows you to perform post-processing tasks after an Advanced Custom Fields (ACF) field group has been saved. This filter is triggered whenever a post is saved or updated, giving you the opportunity to manipulate the data before it is stored in the database.

One common use case for the acf/save_post filter is to perform additional validation or manipulation on ACF field values before they are saved. For example, you may want to sanitize user input, validate certain fields, or perform calculations based on the field values.

Here’s an example code snippet that demonstrates how to use the acf/save_post filter to perform post-processing tasks:

function wpsnippets_acf_save_post( $post_id ) {
    // Check if this is a valid ACF field group
    if ( ! function_exists( 'acf_get_field_group' ) ) {
        return;
    }

    $field_group = acf_get_field_group( 'your_field_group_key' );

    // Perform post-processing tasks only for specific field groups
    if ( $field_group && $field_group['key'] === 'your_field_group_key' ) {
        // Get the field values
        $field_value = get_field( 'your_field_name', $post_id );

        // Perform your post-processing tasks here
        // ...

        // Update the field value if needed
        update_field( 'your_field_name', $field_value, $post_id );
    }
}
add_action( 'acf/save_post', 'wpsnippets_acf_save_post', 20 );

In this example, we define a custom function wpsnippets_acf_save_post that is hooked into the acf/save_post action using add_action. Inside the function, we first check if the ACF plugin is active by checking if the acf_get_field_group function exists. Then, we retrieve the field group using acf_get_field_group and check if it matches the desired field group key.

Next, we retrieve the value of a specific ACF field using get_field and store it in the $field_value variable. This is where you can perform any necessary post-processing tasks on the field value.

Finally, we update the field value using update_field if any modifications were made during the post-processing tasks.

Remember to replace 'your_field_group_key' with the actual key of your ACF field group, and 'your_field_name' with the name of the ACF field you want to process.

By using the acf/save_post filter, you can easily extend the functionality of ACF and perform custom post-processing tasks on ACF field values before they are saved.

Examples

Example 1: Updating a custom field value after saving a post

This use case demonstrates how to update a custom field value after saving a post using the acf/save_post filter. In this example, we will update the value of a custom field called “featuredimageurl” with the URL of the featured image attached to the post.

function wpsnippets_update_featured_image_url( $post_id ) {
    if ( has_post_thumbnail( $post_id ) ) {
        $image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'full' )[0];
        update_field( 'featured_image_url', $image_url, $post_id );
    }
}
add_action( 'acf/save_post', 'wpsnippets_update_featured_image_url', 20 );

Explanation:

  • The wpsnippets_update_featured_image_url function is hooked to the acf/save_post filter using the add_action function.
  • Inside the function, we check if the post has a featured image using has_post_thumbnail.
  • If a featured image is found, we retrieve its URL using wp_get_attachment_image_src and update the value of the “featuredimageurl” custom field using update_field.

Example 2: Sending an email notification after saving a post

This use case demonstrates how to send an email notification after saving a post using the acf/save_post filter. In this example, we will send an email to the site administrator with the post title and permalink.

function wpsnippets_send_post_notification( $post_id ) {
    $post_title = get_the_title( $post_id );
    $post_permalink = get_permalink( $post_id );
    $to = get_option( 'admin_email' );
    $subject = 'New Post Published: ' . $post_title;
    $message = 'A new post has been published on your website. Title: ' . $post_title . ' Permalink: ' . $post_permalink;
    wp_mail( $to, $subject, $message );
}
add_action( 'acf/save_post', 'wpsnippets_send_post_notification', 20 );

Explanation:

  • The wpsnippets_send_post_notification function is hooked to the acf/save_post filter using the add_action function.
  • Inside the function, we retrieve the post title and permalink using get_the_title and get_permalink.
  • We get the site administrator’s email address using get_option('admin_email').
  • We construct the email subject and message.
  • Finally, we use the wp_mail function to send the email.

Example 3: Updating a post meta value after saving a post

This use case demonstrates how to update a post meta value after saving a post using the acf/save_post filter. In this example, we will update the value of a post meta field called “views_count” by incrementing it by 1.

function wpsnippets_update_views_count( $post_id ) {
    $views_count = get_post_meta( $post_id, 'views_count', true );
    $views_count++;
    update_post_meta( $post_id, 'views_count', $views_count );
}
add_action( 'acf/save_post', 'wpsnippets_update_views_count', 20 );

Explanation:

  • The wpsnippets_update_views_count function is hooked to the acf/save_post filter using the add_action function.
  • Inside the function, we retrieve the current value of the “views_count” post meta field using get_post_meta.
  • We increment the value by 1.
  • Finally, we update the post meta field with the new value using update_post_meta.
Last updated on September 14, 2023. Originally posted on September 13, 2023.

Leave a Reply

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