Email to admin whenever member updates custom fields

I need to have an email sent to the admin whenever a member edits any custom field in their profile, showing a list of the “before and after” values related to the update.

There is some info in WP Codex about a hook for comparing old values to new values here:
https://codex.wordpress.org/Plugin_API/Action_Reference/profile_update

…and I’m using code from an article here:

Below is an example based on the above site’s code, of some actions & functions needed for custom fields – however, the code does not work…

// IF HOME PHONE NUMBER CHANGES
function sr_user_profile_update_home_ph( $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->home-ph != $user->home-ph) {
$admin_email = "me@you.org";
$message = sprintf( __( ‘This minister has updated their profile on the Ministers site.’ ) ) . “\r\n\r\n”;
$message .= sprintf( __( ‘Minister Name: %s’ ), $user->display_name ). “\r\n\r\n”;
$message .= sprintf( __( ‘Old Home Phone: %s’ ), $old_user_data->home-ph ). “\r\n\r\n”;
$message .= sprintf( __( ‘New Home Phone: %s’ ), $user->home-ph ). “\r\n\r\n”;
wp_mail( $admin_email, sprintf( __( ‘[Ministers Site] User Profile Update’ ), get_option(‘blogname’) ), $message );
}

}
add_action( ‘profile_update’, ‘sr_user_profile_update_home_ph’, 10, 2 );

// IF CELL PHONE NUMBER CHANGES
function sr_user_profile_update_cell_ph( $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->cell-ph != $user->cell-ph) {
$admin_email = "me@you.org";
$message = sprintf( __( ‘This minister has updated their profile on the Ministers site.’ ) ) . “\r\n\r\n”;
$message .= sprintf( __( ‘Minister Name: %s’ ), $user->display_name ). “\r\n\r\n”;
$message .= sprintf( __( ‘Old Cell Phone: %s’ ), $old_user_data->cell-ph ). “\r\n\r\n”;
$message .= sprintf( __( ‘New Cell Phone: %s’ ), $user->cell-ph ). “\r\n\r\n”;
wp_mail( $admin_email, sprintf( __( ‘[Ministers Site] User Profile Update’ ), get_option(‘blogname’) ), $message );
}

}
add_action( ‘profile_update’, ‘sr_user_profile_update_cell_ph’, 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 );

Does anyone have any advice for me on getting access to the custom fields in a comparison of old value versus newly updated value?

Thanks in advance for any and all help or advice…

-Doug

Don’t know if you are still looking for an answer on this, but I think I figured out part of why it didn’t work.

In this code, the old data is being saved when show_user_profile fires. The problem is that when using [s2Member-Profile /] to display the profile, show_user_profile never fires so the code never runs. What I ended up doing was saving the data on ws_plugin__s2member_before_handle_profile_modifications. You then create your email in ws_plugin__s2member_during_handle_profile_modifications, and clear the data in ws_plugin__s2member_after_handle_profile_modifications.

Hope this helps.

1 Like

Thanks and sorry for the late response. I never received any notification of a reply, and I just now saw the reply.

I might try to get back down into the code to see if I can get something working. We’ve suffered for 2 years without anything working. It’s been a real headache.

Thanks again.

Any chance you might be willing to show actual code and explaining where to put it?

1 Like

@dougjoseph -

We did this. I remember it being a bit tricky. I think we resorted to using 2 or more hooks and a transient just to get it working. I will try to dig out some code to see if it can help you.

We would love to hear if someone has a hook that exposes the old and new values for custom fields at the same time. That would provide a slightly more robust solution.

2 Likes

Thank you, that help would be wonderful!!!

@dougjoseph - So I dug out the code, but it is in the middle of a very complex, 1,300 line, notification system. I will try to extract the good bits and post them for you. It might take a day or so, but I did not forget.

1 Like

You are so kind!!! Thank you!

1 Like