1

I am using wp_insert_user to insert new users and before inserting the user it checks whether the user exists or not and only the new user are registered. Now I want to notify both users and admins about the user registration. Here is the code of wp_insert_user :

$user = $mail['header']->fromaddress;
$email = strstr($mail['header']->fromaddress, '<',true);
for ($j=1; $j < $total; $j++) { 
    $mail = $emails-> get($j);
if(email_exists($email)){
    add_post_meta($post_id, 'ticket_username', $user, True);    
    add_post_meta($post_id, 'ticket_email', $email, True);
 }else{
         $userdata = array(
        'user_login'    => ucwords(strstr($mail['header']->fromaddress, '<',true)),
        'user_pass'     => '',
        'user_nicename' => ucwords(strstr($mail['header']->fromaddress, '<',true)),   
        'display_name'  => ucwords(strstr($mail['header']->fromaddress, '<',true)),
        'user_email'    => strstr($mail['header']->fromaddress, '<'),
        'role'          => 'support_customer',
    );
    $user_id = wp_insert_user($userdata);  
            if ( ! is_wp_error( $user_id ) ) {
                 wp_update_post( array(
                  'ID'          => $post_id,
                  'post_author' => $user_id,
    ) );
}
}
}

And here is the code for notifying users and admins:


if ( !function_exists( 'wp_new_user_notification' ) ) {
  function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {
        $user            = new WP_User($user_id);
        $user_data       = get_userdata( $user_id );
        $firstname       = $user_data->first_name;
        $user_login      = stripslashes( $user_data->user_login );
        
        // URLs
        $site_url  = site_url();
        $ads_url   = site_url( 'ads/' );
        $login_url = site_url();
         
        // Email variables
        $headers            = 'From: EXAMPLE.INFO <[email protected]>' . "rn";
        $blog_name          = get_option( 'blogname' );
        $admin_subject      = 'New User Registration on ' . $blog_name;
        $welcome_subject    = 'Welcome to My site!';
        $welcome_email      = stripslashes( $user_data->user_email );
        $admin_email        = get_option('admin_email');
       
        $admin_message =
<<<EOT
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head><body>
<div class="content">
    <div class="wrapper">
        <p>New user registration on your blog: {$blog_name}.</p>
        <p>Username: {$user_login}</p>
        <p>Email: {$welcome_email}</p>
    </div>
</div>
</body></html>
EOT;
        $welcome_message =
<<<EOT
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head><body>
<div class="content">
<div class="wrapper">
<table width="100%"><tr><td>
Hello {$firstname},<br />
To log into your account, 
go <a href="{$login_url}">visit our site</a> 
and use the credentials below.<br />
Your Username: {$user_login}<br />
Your Password: {$plaintext_pass}<br />
</td></tr></table>
</div>
</div>
</body></html>
EOT;
print_r($admin_email);
//die()
        wp_mail( $admin_email, $admin_subject, $admin_message, $headers );
        wp_mail( $welcome_email, $welcome_subject, $welcome_message, $headers );

    } // End of wp_new_user_notification()
}

This code is not working I guess as it is not sending any emails. I am quite new to all this, so I am hoping to get some help here.

1 Answer 1

0

If you're operating under the assumption that wp_insert_user() sends the notification, then that's where the problem is. That function just inserts the user - it does not by itself send any emails.

You can use the function wp_send_new_user_notifications( $user_id ) to send user notification. However, you need to trigger it based on the result of your wp_insert_user(). Check to make sure you have a user ID and not an error in your result:

if ( ! is_wp_error( $user_id ) ) {
     wp_send_new_user_notifications( $user_id );
} else {
     // you have an error...
}

Note that the wp_send_new_user_notifications() function will send to the user and an admin notification by default (so you don't have to specify "both").

5
  • This creates a critical error on the websites.
    – Rohit kc
    Feb 16, 2022 at 11:34
  • That would likely be due to a typo the function name - wp_send_new_user_notifications() - ending in "s", which I missed, sorry.
    – butlerblog
    Feb 20, 2022 at 22:21
  • Yes, but this doesn't fix anything. The new user notifications are not working.
    – Rohit kc
    Feb 22, 2022 at 10:17
  • "Not working" is too broad - you need to know if you're not getting to where you send the email or if your issue is and email sending issue - those are two entirely different things. Have you verified that you get to wp_send_new_user_notifications() in your code execution?
    – butlerblog
    Feb 28, 2022 at 13:50
  • wp_send_new_user_notifications() is not sending any emails. while I have checked wp_mail is working properly on my plugin.
    – Rohit kc
    Mar 2, 2022 at 4:56

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.