0

I've implemented some meta fields for users like mobile number, address. It can be updated by user as well as admin, I wan't to trigger an email to user and admin stating about the updated field value if any value is updated, irrespective of who updates it.

1

WordPress triggers a custom hook when certain meta is updated, user meta counts. See updated_{$meta_type}_meta. Here's an example use case:

add_action('updated_user_meta', function( $meta_id, $user_id, $meta_key, $_meta_value ) {
    switch ( $meta_key ) {
        case 'mobile_number':
        case 'address':

            $user = new \WP_User($user_id);

            // user
            wp_mail($user->user_email, 'Profile updated', 'Profile updated.');

            // admin
            wp_mail(get_bloginfo('admin_email'), 'Profile updated', "Profile updated for user {$user->display_name}.");
            break;
    } 
}, 0, 4);
|improve this answer|||||
  • email is not generating. – Akshat Apr 24 '17 at 8:52
  • make sure you have the right $meta_key. Read the code and adjust it before posting comments or at least provide what you did. – Samuel Elh Apr 24 '17 at 9:47
  • The meta_key for my mobile number field is phone. I've added this snippet in functions.php add_action('updated_user_meta', function( $meta_id, $user_id, $meta_key, $_meta_value ) { switch ( $meta_key ) { case 'phone': $user = new WP_User($user_id); // user wp_mail($user->user_email, 'Profile updated', 'Profile updated.'); // admin wp_mail(get_bloginfo('admin_email'), 'Profile updated', "Profile updated for user {$user->display_name}."); break; } }, 0, 4); – Akshat Apr 24 '17 at 12:29
0

You can use the profile_update hook to perform actions on post save

function wpse_264096_send_email( $user_id, $old_user_data ) {

    // Get the user object for $user_id
    $user = get_user_by( 'ID', $user_id );
    $user_email = $user->data->email;

    /** You can get any meta fields by this, just change 'meta_key' to the key desired
      * True means its a single value, it will output as a string.
      * If you set to false you get an array with the meta_value, handy if there are multiple values.
      */
    $user_meta = get_user_meta( $user_id, 'meta_key', true );

    // Send e-mail to the user
    $message = "Your information has changed. Your new <META KEY> is now <$user_meta>";
    wp_mail( $user_email, 'Your information has been updated', $message );

    // Send e-mail to the site admin
    $message = "A users information has changed. The new <META KEY> is now <$user_meta>";
    wp_mail( get_option( 'admin_email' ), 'Your information has been updated', $message );

}
add_action( 'profile_update', 'wpse_264096_send_email', 10, 2 );
|improve this answer|||||
  • It's not related to a post, its related with user's meta value, I need to trigger email on "update_user_meta". – Akshat Apr 19 '17 at 13:01
  • What, if I need to include the changed field details, for example if admin has changed mobile number for any user, so how can I generate an email with the new value to the user? – Akshat Apr 19 '17 at 22:01
  • The second paramter is the old user data, meaning this hook is fired after the update process. Meaning you can get the user object and any meta value like you would normally do. I've updated my answer again to help you out with two emails, one to the user and one to the site admin. You can literally just do whatever in this function really. – Jeffrey von Grumbkow Apr 20 '17 at 10:02
  • How can I compare between old and new user data, and find which field's value is changed? Currently email generates even if no value is changed. – Akshat Apr 24 '17 at 9:23
  • The old data is right there and how to get the new data is already in my code as an example. Comparing two pieces of data is really PHP 101 – Jeffrey von Grumbkow Apr 24 '17 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.