Develop

WordPress: Send Admin Notification when User Updates Specific Profile Fields

Background

I’ve had some requests in the past from clients who want to be notified when WordPress users change information in their user profile, easy enough for the most part. However, a recent client asked to be notified when specific information was changed, mainly, contact related info. This would include their email address, their phone number and their mailing address. No big deal, or so I thought…

Rename Post

fig 1.0 send mail

The Issue

This simple idea turned out to be not-so-straightforward. Setting up the conditions for notification on email change were easy: compare $old_user_data->user_email to $user->user_email*, if the values are not equal, send an email. Great!

*more about profile updates in the codex.

However, when using this method with the phone field and any of the address fields $old_user_data returns null. Why?! After some exhaustive searching, I found this little bit of information that explained everything:

$old_user_data is an object containing only the basic WP_User fields, but none of the additional user keys (first_name, last_name, etc. are not part of this object).

Ahhhh, ok. Well at least that explains it. So, what to do now? I thought about if for a bit, but no easy solution came to mind. I also wanted to implement a solution that was native to WordPress, without using jQuery etc. I posted the situation to the Advanced WordPress Facebook Group, which if you’re not involved in, you should be!

Within a matter of minutes I had multiple ideas coming in from awesome WordPress folks. One solution in particular stood out to me, it was a suggestion from Nick Ciske, to store the user meta in a transient, and then compare the data. A transient? I had never heard of this before so I looked it up in the codex, behold, awesome: Transient API.

I was super excited, this is what I needed! I went to bed happy knowing that I had a solution to integrate the next day. When I awoke the next morning I had a message from Nick, he had personally taken the code sample I had posted on GitHub and coded the transient portions for me! Wow…what and awesome and selfless thing to do :)

The Solution

Since this is nowhere to be found on the interwebs, below is the solution I came up with including Nick’s awesome code for setting up the transient metadata. There is an email to be sent for each of the following conditions, when they are true:

1. A user’s email is updated
2. A user’s phone number is updated
3. Any of the address related fields are updated

/* ===========================================
    Send Emails when User Profile Changes
   =============================================*/

// IF EMAIL CHANGES
function sr_user_profile_update_email( $user_id, $old_user_data ) {

  $user = get_userdata( $user_id );
  if($old_user_data->user_email != $user->user_email) {
    $admin_email = "you@yourdomain.com";
    $message = sprintf( __( 'This user has updated their profile on the SchoolRise USA Staff Member site.' ) ) . "\r\n\r\n";
    $message .= sprintf( __( 'Display Name: %s' ), $user->display_name ). "\r\n\r\n";
    $message .= sprintf( __( 'Old Email: %s' ), $old_user_data->user_email ). "\r\n\r\n";
    $message .= sprintf( __( 'New Email: %s' ), $user->user_email ). "\r\n\r\n";
    wp_mail( $admin_email, sprintf( __( '[Staff Member Site] User Profile Update' ), get_option('blogname') ), $message );
  }

}

add_action( 'profile_update', 'sr_user_profile_update_email', 10, 2 );

// IF PHONE NUMBER CHANGES
function sr_user_profile_update_phone( $user_id, $old_user_data ) {

  $old_user_data = get_transient( 'sr_old_user_data_' . $user_id );
  $user = get_userdata( $user_id );

  if($old_user_data->phone != $user->phone) {
    $admin_email = "you@yourdomain.com";
    $message = sprintf( __( 'This user has updated their profile on the SchoolRise USA Staff Member site.' ) ) . "\r\n\r\n";
    $message .= sprintf( __( 'Display Name: %s' ), $user->display_name ). "\r\n\r\n";
    $message .= sprintf( __( 'Old Phone: %s' ), $old_user_data->phone ). "\r\n\r\n";
    $message .= sprintf( __( 'New Phone: %s' ), $user->phone ). "\r\n\r\n";
    wp_mail( $admin_email, sprintf( __( '[Staff Member Site] User Profile Update' ), get_option('blogname') ), $message );
  }

}

add_action( 'profile_update', 'sr_user_profile_update_phone', 10, 2 );

// IF ADDRESS CHANGES
function sr_user_profile_update_address( $user_id, $old_user_data ) {

  $old_user_data = get_transient( 'sr_old_user_data_' . $user_id );
  $user = get_userdata( $user_id );

  if($old_user_data->street != $user->street or $old_user_data->city != $user->city or $old_user_data->state != $user->state or $old_user_data->zip != $user->zip) {

    $admin_email = "you@yourdomain.com";
    $message = sprintf( __( 'This user has updated their profile on the SchoolRise USA Staff Member site.' ) ) . "\r\n\r\n";
    $message .= sprintf( __( 'Display Name: %s' ), $user->display_name ). "\r\n\r\n";
    $message .= sprintf( __( 'Old Address: %s, %s, %s %s' ), $old_user_data->street, $old_user_data->city, $old_user_data->state, $old_user_data->zip ). "\r\n\r\n";
    $message .= sprintf( __( 'New Address: %s, %s, %s %s' ), $user->street, $user->city, $user->state, $user->zip ). "\r\n\r\n";
    wp_mail( $admin_email, sprintf( __( '[Staff Member Site] User Profile Update' ), get_option('blogname') ), $message );
  }

}

add_action( 'profile_update', 'sr_user_profile_update_address', 10, 2 );

// Save old user data and meta for later comparison for non-standard fields (phone, address etc.)
function sr_old_user_data_transient(){

  $user_id = get_current_user_id();
  $user_data = get_userdata( $user_id );
  $user_meta = get_user_meta( $user_id );

  foreach( $user_meta as $key=>$val ){
    $user_data->data->$key = current($val);
  }

  // 1 hour should be sufficient
  set_transient( 'sr_old_user_data_' . $user_id, $user_data->data, 60 * 60 );

}
add_action('show_user_profile', 'sr_old_user_data_transient');

// Cleanup when done
function sr_old_user_data_cleanup( $user_id, $old_user_data ){
  delete_transient( 'sr_old_user_data_' . $user_id );
}
add_action( 'profile_update', 'sr_old_user_data_cleanup', 1000, 2 );

Conclusion

In conclusion, code is awesome. Code that works is more awesome. And people in the WordPress community are the most awesome of them all. Thanks again, Nick!

{ 68 Comments }

  1. Yulius Adrianto

    Hi, Chris.. Great Post !

    Just a little suggestion, maybe it would be better if you pass the $user argument to the sr_old_user_data_transient function. By doing so you can broaden the usage of the transient, for example add “edit_user_profile” action hook.

    And the code will look like this:

    // Save old user data and meta for later comparison for non-standard fields (phone, address etc.)
    function sr_old_user_data_transient( $user ){

    $user_data = get_userdata( $user->ID );
    $user_meta = get_user_meta( $user->ID );

    foreach( $user_meta as $key=>$val ){
    $user_data->data->$key = current($val);
    }

    // 1 hour should be sufficient
    set_transient( ‘sr_old_user_data_’ . $user->ID, $user_data->data, 60 * 60 );

    }
    add_action( ‘show_user_profile’, ‘sr_old_user_data_transient’ );
    add_action( ‘edit_user_profile’, ‘sr_old_user_data_transient’ );

    But according to your client requirement, your code will do just fine.
    Well, it’s only just my suggestion.
    Once again it’s a great post, cheers.

    • Chris Perryman

      Great suggestion! Yes, this could be improved in many ways…I think your suggestion will be very useful to others who need this for wider use, thanks!!

  2. Mark

    Would it be possible to have this work the opposite way, so that it sends the user an email when a specific custom field profile field is edited.

    Thanks
    Mark

    • Chris Perryman

      You can test this, but changing the lines where you see $admin_email (lines 10, 29, 49) to the following should work:

      $user_email = $user->user_email;
      

      You also need to update these lines (15, 34, 54) where is says $admin_email to say $user_email:

  3. Shyam Gupta

    Works fine with email changes but not for custom fields. For custom fields, old data comes out blank and new data shows old data.

    • Chris Perryman

      Hmmm…I’m not sure why you’re having this issue. If you want to post your code into a gist I can try to take a look.

  4. Hailey Nolan

    I got stuck trying to implement this. Im more of a designer than a developer. In the end, http://www.totalwpsupport.com did the job for me as part of a maintenance plan i was already paying for! ha ha

    • Chris Perryman

      They implemented my code example?

  5. Jamie Aguinaldo

    Hi! This is great! However, can I use this for custom fields?

    • Chris Perryman

      Yes, this will work with your custom fields ;)

{ Respond }

Leave a response