不带插件的用户注册表中的电子邮件确认

时间:2019-11-08 作者:priya

我已经在自定义模板中为用户创建了一个HTML注册表单。现在我想在注册的电子邮件中发送一个电子邮件确认链接,注册用户可以从中登录。

下面是我的代码。注册已成功完成。如何生成确认链接?

if($_POST){
     $username= $wpdb->escape($_POST[\'txtusername\']);
     $email= $wpdb->escape($_POST[\'txtEmail\']);
    $password= $wpdb->escape($_POST[\'txtpassword\']);
     $confpassword= $wpdb->escape($_POST[\'txtconfirmpassword\']);

     $error=array();

     if(strpos($username,\' \')!==FALSE){
       $error[\'username_space\']="username has space";

     }

     if(empty($username)){
         $error[\'username_empty\']="username needed";
     }
     if(username_exists($username)){
     $error[\'username_exist\']="username already exists";

     }

  if(!is_email($email)){
      $error[\'email_valid\']="enter valid email id";

     }

     if(email_exists($email)){
      $error[\'email_existence\']="email already exists";

     }

     if(strcmp($password, $confpassword)){
      $error[\'password\']="password doesnt match";

     }

     if(count($error)==0) {
      wp_create_user($username,$password,$email);
      echo "you have registered succesfully.. ";

    $new_user = wp_insert_user( $userdata );
    wp_new_user_notification($username,$password,$email);
    exit();
     }

     else{
        print_r($error);
        }
}

1 个回复
最合适的回答,由SO网友:butlerblog 整理而成

实际上,您传递给wp_new_user_notification() 函数不正确-它需要用户ID,而不是用户名。此外,在这种情况下,这可能不是您想要使用的,因为它会生成一个链接,要求用户设置密码(这是首选)。

我将介绍两个示例—一个没有密码,用户在确认后会设置密码;还有一个带有密码,这需要使用自定义电子邮件,而不是wp_new_user_notification().

Preferred Method (no password)

这假设您将删除表单中的密码字段,并允许用户在通过新用户通知电子邮件中包含的链接返回时设置其密码。代码之后讨论了其他问题:

if($_POST){
     $username= sanitize_user($_POST[\'txtusername\']);
     $email= sanitize_email($_POST[\'txtEmail\']);

     $error=array();

     if(strpos($username,\' \')!==FALSE){
       $error[\'username_space\']="username has space";

     }

     if(empty($username)){
         $error[\'username_empty\']="username needed";
     }
     if(username_exists($username)){
     $error[\'username_exist\']="username already exists";

     }

  if(!is_email($email)){
      $error[\'email_valid\']="enter valid email id";

     }

     if(email_exists($email)){
      $error[\'email_existence\']="email already exists";

     }

     if(count($error)==0) {
      $user_id = wp_create_user($username,$password,$email);
      echo "you have registered succesfully.. ";

    wp_new_user_notification($user_id);
    exit();
     }

     else{
        print_r($error);
        }
}
此处需要注意的重要更改:

您使用的转义方法是专门针对WPDB对象的。相反,在处理已发布的表单数据时,需要进行清理。有适当的WP函数用于此,包括我在您的函数中更改的两个-sanitize_user()sanitize_email().wp_create_user() 然后立即使用wp_insert_user(). 除非在此代码段之外有代码正在组装$userdata 您在该函数中拥有的值,this is unnecessary. wp_create_user() 实际运行wp_insert_user() 然后返回用户ID。所以我删除了那个不必要的函数(因为它传递的数据在代码中是一个未定义的变量)wp_new_user_notification()incorrect. 该函数接受两个由不再使用的弃用参数分隔的参数。所以$user_id (不是用户名),空的不推荐使用的参数(由于忽略了该参数,因此为空),然后是可选的$notify (空、用户、管理员或两者兼有)-wp_new_user_notification( $user_id, \'\', \'\'). 这个$user_id 价值来自wp_create_user(). 我省略了后两个参数,因为一个被函数忽略,另一个是可选的(如果未设置,它将只发送给用户)请仔细注意上面的#3,因为您向该函数发送的内容可能就是您遇到问题的原因。首先,您传递的是用户的用户名而不是用户ID,因此函数将永远找不到用户。但更重要的是,如果您查看实际函数,如果第三个参数不是“user”、“admin”、“both”或“default”,则函数将立即退出。您在此位置传递用户的电子邮件,因此该功能将立即退出。

我已经链接了这些函数的文档-在使用WP核心函数时,最好查看这些信息,以确保正确使用它们。

Alternate method:

这是我答应过的另一种方法。这纠正了类似的问题,例如仅使用wp_create_user(). 但不同的是,您需要创建并发送自己的自定义电子邮件,因为wp_new_user_notification() 函数假定您正在发送一个链接,供用户设置密码(您已经在表单中收集了密码)。

为了发送您的自定义电子邮件,它使用wp_mail() (与上述代码一样,您应该花一些时间查看函数的文档,以便了解它的功能和使用方法)。

if($_POST){
     $username= sanitize_user($_POST[\'txtusername\']);
     $email= sanitize_email($_POST[\'txtEmail\']);
    $password= $_POST[\'txtpassword\'];
     $confpassword= $_POST[\'txtconfirmpassword\']);

     $error=array();

     if(strpos($username,\' \')!==FALSE){
       $error[\'username_space\']="username has space";

     }

     if(empty($username)){
         $error[\'username_empty\']="username needed";
     }
     if(username_exists($username)){
     $error[\'username_exist\']="username already exists";

     }

  if(!is_email($email)){
      $error[\'email_valid\']="enter valid email id";

     }

     if(email_exists($email)){
      $error[\'email_existence\']="email already exists";

     }

     if(strcmp($password, $confpassword)){
      $error[\'password\']="password doesnt match";

     }

     if(count($error)==0) {
      $user_id = wp_create_user($username,$password,$email);
      echo "you have registered succesfully.. ";

        $subject = \'Thanks for registering\';
        $message = \'Thanks for registering. 

            Please return to the site to log in:

            https://example.com/my-login/\';

        wp_mail( $email, $subject, $message );

        exit();
     }

     else{
        print_r($error);
        }
}

相关推荐