0

currently using this plugin https://wordpress.org/plugins/wp-rest-user/ and there is a section where if you want to send a user an email when he/she registers (using wordpress as a headless CMS)

add_action('wp_rest_user_user_register', 'user_registered');
function user_registered($user) {
    // Do Something
}

already set up WP Mail SMTP plugin so it overrides the wp_mail function

the question is: how can I get the email of the registered user to send him/her an email using the wp_mail function ?

2
  • Have you checked, what exactly $user is you're getting passed? If it is an object of the WP_User class, it should be trivial to get their mail address.
    – kero
    Commented May 22, 2019 at 16:11
  • @kero I think it is a WP_User object but how can I make sure it is? like is there a way to find out? Commented May 22, 2019 at 17:21

1 Answer 1

0

You don't know the type of content that might be passed for the $user parameter, so let's test it out.

You will want to expand on these conditions and responses. This is just an example of the tests you probably want to make.

function user_registered( $user ) {
    // Check to make sure it's not an error.
    if ( is_wp_error( $user ) ) {
        return;
    }
    // This is how WordPress checks to make sure the user exists.
    // This could also apply to many other objects, though. 
    if ( ! isset( $user->ID ) ) {
        return; 
    }
    // This checks to make sure you're getting the expected user object fields.
    if ( ! isset( $user->user_email ) ) {
        // If you have a correct ID, you can still retrieve the user's fields.
        $user = get_user_by( 'ID', $user->ID );
        // If the user doesn't exist, stop where you are.
        if ( ! $user ) {
            return false;
        }
    }

    $headers = array(
        'Content-Type: text/html; charset=UTF-8',
        'From: WordPress Website <[email protected]>',
    );
    wp_mail( $user->user_email, 'Subject of email', 'Email body', $headers );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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