I want to modularly disable emails sent out by WordPress so I can replace them with my own custom emails when necessary.
This is my custom Welcome Email:
function send_welcome_email_to_new_user($user_id) {
$user = get_userdata($user_id);
$user_email = $user->user_email;
// for simplicity, lets assume that user has typed their first and last name when they sign up
$user_full_name = $user->user_firstname . $user->user_lastname;
// Now we are ready to build our welcome email
$to = $user_email;
$subject = "Hi " . $user_full_name . ", welcome to our site!";
$body = '
<h1>Dear ' . $user_full_name . ',</h1></br>
<p>Thank you for joining our site. Your account is now active.</p>
<p>Please go ahead and navigate around your account.</p>
<p>Let me know if you have further questions, I am here to help.</p>
<p>Enjoy the rest of your day!</p>
<p>Kind Regards,</p>
<p>poanchen</p>
';
$headers = array('Content-Type: text/html; charset=UTF-8');
if (wp_mail($to, $subject, $body, $headers)) {
error_log("email has been successfully sent to user whose email is " . $user_email);
}else{
error_log("email failed to sent to user whose email is " . $user_email);
}
}
When I create a new user, I got 2 email notification: my own custom email and wordpress default email. I want to hide the wordpress default email from the inbox.
How can I disable WordPress emails notifications to achieve this?
- Thank you for signing up/New User registration email
CodePudding user response:
To disable WordPress default emails notifications
Please edit the function.php file and add the following code
<?php
//Disable the new user notification sent to the site admin
function wp_disable_new_user_notifications() {
//Remove original use created emails
remove_action( ‘register_new_user’, ‘wp_send_new_user_notifications’ );
remove_action( ‘edit_user_created_user’, ‘wp_send_new_user_notifications’, 10, 2 );
}
then
add_action( ‘init’, ‘wp_disable_new_user_notifications’ );
However, please be careful in using the above so that the remove_action will not affect any other functionalities you may wish to use in WP
CodePudding user response:
Instead of creating an another email for the same, you can also customize WordPress default emails using wp_new_user_notification()
function.
add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );
function custom_wp_new_user_notification_email(
$wp_new_user_notification_email, $user, $blogname ) {
$user_first_name = stripslashes( $user->first_name );
$user_last_name = stripslashes( $user->last_name );
$user_full_name = $user_first_name .' '. $user_last_name;
$user_email = stripslashes( $user->user_email );
$wp_new_user_notification_email['subject'] = "Hi " . $user_full_name . ", welcome to our site!";
$message = '
<h1>Dear ' . $user_full_name . ',</h1></br>
<p>Thank you for joining our site. Your account is now active.
</p>
<p>Please go ahead and navigate around your account.</p>
<p>Let me know if you have further questions, I am here to help.
</p>
<p>Enjoy the rest of your day!</p>
<p>Kind Regards,</p>
<p>poanchen</p>
';
return $wp_new_user_notification_email;
}
Learn more about wp_new_user_notification()
Here