Support

Account

Home Forums ACF PRO Admin notification of field change

Solving

Admin notification of field change

  • Is it possible to notify the WP-Administrator when certain fields are changed?

  • You could create an ACF/Save_post filter that runs both before and after acf. In the first run, het the current values and store it. In the second run compare the new value with the stored value. If not the same then send an email t the site admin.

  • Brand new here and not much of a coder. Do you know where I could get started learning what you described? I don’t know how the filter would look. I don’t know what before and after ACF means in practice. I don’t know how to compare (what that command would be) and how to send an email to the admin. I follow your thought process and assume it could work but am very basic in my PHP skills.

    I’m asking for something I can only vaguely understand so that being said, do know where I might look to get started on any of these items?

    Sorry that I’m very rudimentary.

  • 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
      
    ?>
    
  • That’s amazing, John. Thanks. I’ll have to study it awhile.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Admin notification of field change’ is closed to new replies.