Support

Account

Home Forums ACF PRO Check if field has changed when save to change mail

Solving

Check if field has changed when save to change mail

  • Hi!

    I have a functions which sends an email to the author of a custom-post-type when the post is changed:

        add_action( 'save_post', 'my_project_updated_send_email_custom' );
    
        function my_project_updated_send_email_custom( $post_ID ) {
            $author_id = get_post_field ('post_author', $post->ID);
            $emails  = get_the_author_meta( 'user_email', $author_id);
            $headers = 'From: Suite <[email protected]>';
            $title   = wp_strip_all_tags( get_the_title( $post->ID ) );
            $message = 'New Document saved!';
            if ( get_post_status ( $post->ID )  === 'publish' ) {
                if ( get_post_type( $post->ID ) === 'dokumente' ) {
                    wp_mail( $emails, 'New document for you', $message, $headers );
                }
            }
        }

    All the fields in this custom post type are from advanced custom fields. In total there are 3 fields (all repeater fields -> reports, manuals, tips). Now i would like to change the text of the mail based on which fields are changed. is this possible?

    Thank you so much!

  • To do this you need to set your filter to run on the hook acf/save_post with a priority <10.
    https://www.advancedcustomfields.com/resources/acf-save_post/

    At this point any old values for the custom fields are still in the database and you can compare the values that are submitted with the old values:

    
    $old_value = get_field('your_field_name', $post_id);
    $new_value = $_POST['acf']['your_field_key'];
    

    You’ll also need to adjust your other function to use the post ID passed to your filter by ACF

  • Thank you so much for your help John. It helped me and i tried to get my solution. Im not the greatest programmer on earth and so my solutionis still not working. Can you maybe show me a short example of how i can compare the two field values inside my function and check if they are the same?

    Thank you much and best regards.

  • Let’s say that your field name is “text_field” and it has a field key of “field_123456”
    Notice I’m only using the field key, since you must use the field key for the new values you might just as well use it for both.

    
    add_filter('acf/save_post', 'my_compare_acf_fields', 1);
    function my_compare_acf_fields($post_id) {
      $old_value = get_field('field_123456', $post_id);
      $new_value = '';
      if (isset($_POST['acf']['field_123456'])) {
        $new_value = $_POST['acf']['field_123456'];
      }
      if ($new_value != $old_value) {
        // value has changed, do something
      }
    }
    
  • Hey John, i did exactly as you described. My code is always executing. Is it possible because i use a repeater field which gives me back an array? Is it possible to check if a repeater field is changed?

  • Checking a repeater field will be difficult. Not only to you need to contend with the fact that they are arrays of values, someone could also alter the order of the rows.

    You may also be running into differences between inputted values and stored values that have nothing to do with something being changed. There can be instances where the values in $_POST[‘acf’] contain slashes \

    While comparing arrays is possible, it would be a complicated process and I’m not sure that I can give you a reliable solution for, especially since the order of the rows can be changed. The best possible alternative is the serialize both values and compare that answer here https://stackoverflow.com/questions/7389176/compare-multidimensional-arrays-in-php

  • Ok, thank you.

    I came up with a new solution just to count the number of values inside the two arrays:

        add_action('acf/save_post', 'my_project_updated_send_email_custom', 10);
    
        function my_project_updated_send_email_custom( $post_ID ) {
    
            /* new and old field value */
            $dokumenteOld = get_field('field_5ba11346c0289', $post->ID);
            $dokumenteNew =  '';
            if (isset($_POST['acf']['field_5ba11346c0289'])) {
                $dokumenteNew = $_POST['acf']['field_5ba11346c0289'];
            }
            
            /* get elements in array */
            $countOld = count($dokumenteOld);
            $countNew = count($dokumenteNew);
            
        }

    Now the problem is that both containt the same number of values when i a add a new line in the repeater field (the new count). I also can see the new value in both arrays.

    Sorry, this is the last quesiton i ask i promise. Thank you so much for your help!

  • Your priority must be less than 10, if not then ACF has already save the new values to the DB.

    
    add_action('acf/save_post', 'my_project_updated_send_email_custom', 1);
    
Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.