用于通知用户和管理员新用户注册的WP_NEW_USER_NOTIFICATIONS

时间:2022-02-15 作者:Rohit kc

我正在使用wp_insert_user 要插入新用户,在插入用户之前,它会检查用户是否存在,并且只注册新用户。现在,我想通知用户和管理员有关用户注册的信息。这是的代码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,
    ) );
}
}
}
下面是通知用户和管理员的代码:


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 <info@example.info>\' . "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()
}
我猜这个代码不起作用,因为它没有发送任何电子邮件。我对这一切都很陌生,所以我希望在这里得到一些帮助。

1 个回复
SO网友:butlerblog

如果你的经营假设是wp_insert_user() 发送通知,这就是问题所在。该功能只是插入用户-它本身不发送任何电子邮件。

您可以使用该功能wp_send_new_user_notifications( $user_id ) 发送用户通知。但是,您需要根据wp_insert_user(). 检查以确保您有用户ID,并且结果中没有错误:

if ( ! is_wp_error( $user_id ) ) {
     wp_send_new_user_notifications( $user_id );
} else {
     // you have an error...
}
请注意wp_send_new_user_notifications() 默认情况下,函数将向用户和管理员发送通知(因此您不必指定“两者”)。

相关推荐

如何将外部变量传递给wp_new_User_Notify_Email过滤器?

我们正在尝试自定义新用户在我们的网站上注册时收到的默认电子邮件。为此,我们使用WP4.9.0中添加的wp\\u new\\u user\\u notification\\u email筛选器对于我们的功能。php文件不要凌乱,我们在“templates/email\\u welcome.php”中有电子邮件模板。我们需要加载此文件来构建$message变量,但返回的内容始终为空。下面是我们在子主题函数中添加的内容。php// ========================================