Send automatic mail to Admin when user/member changes/adds profile

Is there a way to send the updated/added values from profile, when a member/user updates his/hers data, to the admin of the site or another emailadress?

Can this be the first step?

/* do something when user edits profile */
add_action('personal_options_update', 'notify_admin_on_update');
function notify_admin_on_update(){
  // send a mail with the updated values to admin@mysite.com
  exit;
}

What is best pracice to send emails from within WordPress?

1 Answer
1

you got the first part right about using personal_options_update but to be on the safe side add edit_user_profile_update also.
and as for sending emails within WordPress the best way would be to use wp_mail, So something like this:

add_action( 'personal_options_update', 'notify_admin_on_update' );
add_action( 'edit_user_profile_update','notify_admin_on_update');
function notify_admin_on_update(){
    global $current_user;
    get_currentuserinfo();

    if (!current_user_can( 'administrator' )){// avoid sending emails when admin is updating user profiles
        $to = 'admin@email.com';
        $subject="user updated profile";
        $message = "the user : " .$current_user->display_name . " has updated his profile with:\n";
        foreach($_POST as $key => $value){
            $message .= $key . ": ". $value ."\n";
        }
        wp_mail( $to, $subject, $message);
    }
}

Leave a Comment