首页 视频课程 WordPress开发文档用户权限与角色 用户注册

用户注册

2020-04-21 / 5795阅

本视频需要登录以后才能播放

登录

WordPress让主题支持前台用户注册视频教程

很多情况下我们希望用户在网站的前台进行注册,能够自定义注册表单和内容,本课程讲解如何在前台进行注册。

$notice = '';
if(isset($_POST['login']) && $_POST['login']){
       //开始处理表单
       if(isset($_POST['mail']) && $_POST['mail']){
              if(isset($_POST['password']) && $_POST['password']){
                     //开始写逻辑的地方,创建用户账户
                     $email = $_POST['mail'];
                     if (email_exists($email)) {
                            $notice =  '电子邮件地址已经被注册';
                     } else {
                            if (username_exists($_POST['login'])) {
                                   $notice =  '用户名已经被注册';
                            } else {
                                   $userdata = array(
                                          'user_login' => $_POST['login'],
                                          'user_email' => $email,
                                          'user_pass' => $_POST['password'],
                                   );
                                   $user_id = wp_insert_user($userdata);
                                   if (!$user_id) {
                                          $notice =  '注册失败,请联系管理员';
                                   } else {
                                          //直接登录用户
                                          wp_set_current_user($user_id, $_POST['login']);
                                          wp_set_auth_cookie($user_id);
                                          do_action('wp_login', $_POST['ws_phone']);
                                          echo 'OK';
                                   }
                            }
                     }
              }else{
                     $notice = '密码不能为空';
              }
       }else{
              $notice = '邮箱地址错误';
       }
}else{
       $notice = '账号不能为空';
} 

阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228

大家谈论
    我的见解
    目录