admin管理员组

文章数量:1129017

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=";><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=";><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.

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.

Share Improve this question edited Feb 15, 2022 at 21:01 butlerblog 5,0813 gold badges26 silver badges42 bronze badges asked Feb 15, 2022 at 14:00 Rohit kcRohit kc 298 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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").

本文标签: plugin developmentwpnewusernotifications to notify user and admin about new user registration