Support

Account

Home Forums ACF PRO Admin notification of field change Reply To: Admin notification of field change

  • documentation for acf/save_post can be found here http://www.advancedcustomfields.com/resources/acfsave_post/

    you would create an action that runs before and after acf does it’s save.

    Before the save you would use get_post_meta(), I think https://developer.wordpress.org/reference/functions/get_post_meta/ to get the current value, but get_field() may also work http://www.advancedcustomfields.com/resources/get_field/

    After acf the save you’d get the value again and test to see if it was changes.

    You would need either build this as a class so that you can store values during the first call and then check them on the second or you would need to store the values from the fist call in a global variable $GLOBALS['my_variable_name']

    I whipped up a quick example: I haven’t really testing it, but I think everything is correct.

    
    <?php 
      
      new notify_on_acf_change();
      
      class notify_on_acf_change {
        
        // array of post types to check
        var $post_types = array(
          'post',
          'page'
        );
        
        // array of fields names to track
        var $fields_to_track = array(
          'field1',
          'field2',
          'field3',
          'field4',
        );
        
        // array to store values before save
        var $before_update = array();
        
        function __construct() {
          add_action('acf/save_post', array($this, 'before_acf'), 1);
          add_action('acf/save_post', array($this, 'after_acf'), 20);
        } // end function __construct
        
        function before_acf($post_id) {
          $post_type = get_post_type($post_id);
          if ($post_type || 
              !in_array($post_type, $this->post_types) ||
              empty($_POST['acf'])) {
            /* bail early if 
                    $post_type is false = new post
                OR  $post_type is not one of those to track
                OR  There is not acf data
            */
            return;
          }
          foreach ($this->fields_to_track as $field) {
            // store the current value of each field
            $this->before_update[$field] = get_post_meta($post_id, $field, true);
          }
        } // end function before_acf
        
        function after_acf($post_id) {
          $post_type = get_post_type($post_id);
          if ($post_type || 
              !in_array($post_type, $this->post_types) ||
              empty($_POST['acf'])) {
            /* bail early if 
                    $post_type is false = new post
                OR  $post_type is not one of those to track
                OR  There is not acf data
            */
            return;
          }
          $changed_values = array();
          foreach ($this->fields_to_track as $field) {
            // store the current value of each field
            $before_value = false;
            if (isset($this->before_update[$field])) {
              $before_value = $this->before_update[$field];
            }
            $after_value = get_post_meta($post_id, $field, true);
            if ($before_value !== $after_value) {
              $changed_values[] = array(
                $field => array($before_value, $after_value)
              );
            }
          }
          if (!count($changed_values)) {
            // nothing changed, bail
            return;
          }
          $to = get_option('admin_email');
          $subject = 'Tracked Fields Changed on '.$_SERVER['HTTP_HOST'];
          $message = 'The following tracked fields were altered:'."\r\n\r\n";
          foreach ($changed_values as $field => $values) {
            $message .= 'Field: '.$field.' - From: '.$values[0].' - To: '.$values[1]."\r\n";
          }
          wp_mail($to, $subject, $message);
        } // end function after_acf
        
      } // end class notify_on_acf_change
      
    ?>