admin管理员组

文章数量:1426216

currently using this plugin / 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 ?

currently using this plugin https://wordpress/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 ?

Share Improve this question asked May 22, 2019 at 16:09 technolaajitechnolaaji 1034 bronze badges 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? – technolaaji Commented May 22, 2019 at 17:21
Add a comment  | 

1 Answer 1

Reset to default 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 );
}

本文标签: pluginsSend email when a user registers Rest api