How to send an email once the user updates his profile (from default user profile & WPUF Pro Profile form)

In this short article, I will show you how to send an email once your users are updating his/her profile. So let get started with the main stage.

Heads Up

  • To execute the operation for the WordPress default use-case, I will use WP profile_update hooks. You can find more details about this action/hook from the wp developer’s doc (click here).
  • And if you are using WP User Frontend Pro on your site and want to send an email to your users while updating their WPUF profile form, I will use wpuf_update_profile action/hook.

Default WordPress Case

You can use the below code on your theme or child theme’s functions.php file to fire an email to the users and site admin to acknowledge the update.

/**
@ Send email to user/admin if user updated his profile
@
**/
function site_user_profile_update( $user_id ) {
        
    $user_info = get_userdata( $user_id );
    
    $to = $user_info->user_email; // This will send email to the user email who is updating profile

    // $to = get_option( 'admin_email' ) .", ".$user_info->user_email; // This will send email to the user email and to your site Administration Email
    
    $subject = "The user Profile Updated";
    
    $message = "Hello (" .$user_info->display_name . ") \n\nYour profile has been updated! \n\nThank you!" . "\n\n";

    $message .= 'Site Address: ' . get_bloginfo('wpurl') . "\n";

    wp_mail( $to, $subject, $message);

}
add_action( 'profile_update', 'site_user_profile_update', 10, 2);

You can manipulate the above function as you want according to your need.

WPUF Pro Profile Form Case

You can use this below code to send email to your users or site admin at the same time. I will also show how you can pass your WPUF custom fields metadata on the email message.

Use Cases

There is a WPUF custom field like Phone or Address, which you want to track as if any user changes those fields you want to know that this user has changed his profile data.

/**
@ Send email to admin & user if user updated his WPUF profile fields
@
**/

function wpuf_user_profile_update_notification( $user_id ) {

    if (!current_user_can( 'administrator' )){// avoid sending emails when admin is updating user profiles

    $user_info = get_userdata( $user_id );

    $to = get_option( 'admin_email' ) .", ".$user_info->user_email;
    $subject = $user_info->display_name . " Profile Updated";

    //Get user custom WPUF fields (i.e: Phone, Address field) meta
    $phone = get_user_meta( $user_id, 'phone', true );
    $address_field = get_user_meta( $user_id, 'address_field', true );

    // Message body
    $message .= "The user (". $user_info->display_name. ") has updated his profile with:". "\n\n";
    
    // Show user custom WPUF fields (i.e: Phone, Address field) meta which you want to show on the email message

    $message .= 'Phone: ' . $phone . "\n";
    // $message .= 'Address: ' . $address_field['street_address'] .", ". $address_field['street_address2'] .", ". $address_field['city_name'] . ", ". $address_field['zip'] . ", ". $address_field['state'] . ", ". $address_field['country_select'] . "\n";
    
    $message .= 'Address:'. "\n";
    $message .= 'Street 1- ' . $address_field['street_address'] . "\n";
    $message .= 'Street 2- ' . $address_field['street_address2'] . "\n";
    $message .= 'City- ' . $address_field['city_name'] . "\n";
    $message .= 'Post/Zip- ' . $address_field['zip'] . "\n\n";

    $message .= 'Site Address: ' . get_bloginfo('wpurl') . "\n";

    wp_mail( $to, $subject, $message);

    }

}
add_action( 'wpuf_update_profile', 'wpuf_user_profile_update_notification', 10, 3 ); //This action fire while  WPUF Profile form update

Side note: WP User Frontend fire the hook do_action( 'wpuf_after_register', $user_id, $userdata, $form_id, $form_settings ); while anyone registers through the WPUF registration form.

So as you can see on the above code, the only difference is the different action/hooks, and I pass user custom metadata to show it on the email message. Again, you can manipulate the function as you need. Only you have to hook the functions with the associate hook which you want.

Wrapping up

I hope this handy tips will helps you mainly if you are using WP User Frontend Pro plugin. So this is all for today.

Peace and Love! ❤️

MM Aurangajeb
MM Aurangajeb

Spreading Happiness. Support Engineer, WordPress Enthusiast, Blogger.

Articles: 18

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.