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