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 |1 Answer
Reset to default 0You 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 );
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745469467a4629068.html
$user
is you're getting passed? If it is an object of theWP_User
class, it should be trivial to get their mail address. – kero Commented May 22, 2019 at 16:11