Support

Account

Home Forums Backend Issues (wp-admin) acf/save_post – execute only when value of field has changed

Solved

acf/save_post – execute only when value of field has changed

    • laju

    • January 20, 2021 at 9:34 pm

    Hi Guys,

    im trying to fire a email based on if a user has changed a specific field.

    currently im trying according to acf/save_post documentation:

    if ( isset($_POST['acf']['field_5fd213a8c6dfe']) )

    it sends the email when the field is changed but also if the field has a value and wasnt changed. only if the field is empty it doesn’t send the email.

    could you help me find a better way to check if the field has changed not only if the field is not empty?

  • You need to run your action with a priority of <10 so that it runs before the new values are saved.
    $_POST['acf']['field_5fd213a8c6dfe'] will have the new value and get_field('field_5fd213a8c6dfe') will return the old value.

    • laju

    • January 21, 2021 at 8:57 pm

    Thanks John

    • laju

    • January 21, 2021 at 9:21 pm

    my action acf/save_post runs with priority of 5 now

    i want to check if the user changed his address field with

      if ( $_POST['acf']['field_5fd213f4c6e02'] != get_field('field_5fd213f4c6e02') ) {
      	$street = $_POST['acf']['field_5fd213f4c6e02'];
      }

    then i send my email:

    if ( !empty( $street ) {
    ....
    }

    but now it sends me an email every time i update. not only if field field_5fd213f4c6e02 is updated.

    • laju

    • January 21, 2021 at 9:24 pm

    here the full code maybe better to see what i have in mind.

    add_action('acf/save_post', 'save_customerdata', 5);
    
    function save_customerdata( $post_id ) {
        
    
      if( is_admin() ) {    
        return;   
      }
    
      if ( $_POST['acf']['field_5fd213a8c6dfe'] != get_field('field_5fd213a8c6dfe') ) {
      	$plz = $_POST['acf']['field_5fd213a8c6dfe'];
      }
    
      if ( $_POST['acf']['field_5fd213f4c6e02'] != get_field('field_5fd213f4c6e02') ) {
      	$street = $_POST['acf']['field_5fd213f4c6e02'];
      }
    
      if ( $_POST['acf']['field_5fd21405c6e03'] != get_field('field_5fd21405c6e03') ) {
      	$ptel = $_POST['acf']['field_5fd21405c6e03'];
      }
    
      if ( $_POST['acf']['field_5fd21410c6e04'] != get_field('field_5fd21410c6e04') ) {
      	$pfax = $_POST['acf']['field_5fd21410c6e04'];
      }
    
      if ( !empty( $plz ) OR !empty( $street ) OR !empty( $ptel ) OR !empty( $pfax ) ) {
        
    
        $post = get_post( $post_id ); 
        
    
        $user_id = str_replace("user_", "", $post_id);  
        
        $user_info = get_userdata($user_id);
        $first_name = $user_info->first_name;
        $last_name = $user_info->last_name;
        $user_email = $user_info->user_email;
        $user_login = $user_info->user_login;
        
        
        $to = 'john.doe@acme.com';
        $headers = array('From: ' . $user_info->first_name. ' '.$user_info->last_name . ' <'.$user_info->user_email.'>');
        $subject = 'Profile Updated';
        $body = $user_info->first_name. ' '.$user_info->last_name. ' hat das Profil aktualisiert' . "\r\n";
        $body .= 'Benutzername: ' . $user_login . "\r\n";
    
        if ( !empty( $plz ) ) {
          $body .= 'PLZ / ORT wurde aktualisiert.' . "\r\n";
        }
        if ( !empty( $street ) ) {
          $body .= 'Strasse / Hausnummer wurde aktualisiert.' . "\r\n";
        }
        if ( !empty( $ptel ) ) {
          $body .= 'Telefonnummer wurde aktualisiert.' . "\r\n";
        }
        if ( !empty( $pfax ) ) {
          $body .= 'Faxnummer wurde aktualisiert.' . "\r\n";
        }
    
          
    
        wp_mail($to, $subject, $body, $headers );
    
      }
      
    }
  • I cannot be sure. $_POST['acf']['field_5fd213f4c6e02'] may be escaped in some way so that even if it’s the same that a simple string comparison may show them to be not equal.

    • laju

    • January 22, 2021 at 8:28 pm

    Hi John,

    i don’t quite understand your last post but i will try to figure it out. if i find a solution i’ll update here for other users.

    thanks for your help!

  • When you submit values through a form on a web page the browser may escape special characters like single quotes. It may also convert html entities. These changes may cause the two compared values to look the same but to actually be different strings when compared.

    However, looking back at your code I thing the reason for your problem is something else that I missed the last time. You need to supply the post ID when getting fields.

    
    if ($_POST['acf']['field_5fd213a8c6dfe'] != get_field('field_5fd213a8c6dfe', $post_id)){
    
Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.