drkrsx3135168 2014-06-18 14:33
浏览 41
已采纳

无法在wordpress环境中以编程方式创建用户

I'm new to wordpress and I'm looking forward to adding users through a front-end register page, I'm really confused right now ecause I cant get this code to work, it just wont create the user.

Here is the code:

<?php
/*
Template Name: Register
*/

get_header(); ?>



<?php
if(isset($_POST['submit'])){

    $email1 = $_POST['email1'];
    $email2 = $_POST['email2'];
    $pass1 = $_POST['pass1'];
    $pass2 = $_POST['pass2'];

    if($email1 == $email2){
        if($pass1 == $pass2)
        {
            if( null == username_exists( $email_address ) ) {
                $user_login = $_POST['uname'];
                $user_email = $_POST['email1'];
                $user_pass = $_POST['pass1'];

              $user_id = wp_create_user( $user_login, $user_pass, $user_email );

              wp_update_user(
                array(
                  'ID'          =>    $user_id,
                  'nickname'    =>    $email_address
                )
              );
              $user = new WP_User( $user_id );
              $user->set_role( 'contributor' );
            }


        }else{
            echo "Sorry, your passwords do not match <br/>";
        }
    }else{
        echo "Sorry your Emails dont match <br/>";
        exit();
    }

}else{
    $form = <<<EOT
    <form action="" method="POST">
        First Name: <input type="text" name="name"/> <br/>
        Last Name:  <input type="text" name="lname"/> <br/>
        Username:   <input type="text" name="uname"/> <br/>
        Email:  <input type="text" nam="email1"/> <br/>
        Confim Email:   <input type="text" name="email2"/> <br/>
        Password:   <input type="password" name="pass1"/> <br/>
        Confirm Password:   <input type="password" name="pass2"/> <br/>
        <input type="submit" value="Register" name="submit"/>
    </form>
EOT;

echo $form;
}

?>

<?php get_footer(); ?>

Thanks.

Not sure if important or not, but I chose wpwpp_ as the prefix

  • 写回答

2条回答 默认 最新

  • douyi1966 2014-06-18 14:52
    关注

    check your function if( null == username_exists( $email_address ) ) //$email_addrress should be either $email1 or $email2

    But i would recomend the wp_insert_user rather than wp_create_user instead..

    <?php
    /*
    Template Name: Register
    */
    
    get_header(); ?>
    
    
    
    <?php
    if(isset($_POST['submit'])){
    
    $email1 = $_POST['email1'];
    $email2 = $_POST['email2'];
    $pass1 = $_POST['pass1'];
    $pass2 = $_POST['pass2'];
    
    if($email1 == $email2){
        if($pass1 == $pass2)
        {
            if( null == username_exists( $email1) ) {
                $user_login = $_POST['uname'];
                $user_email = $_POST['email1'];
                $user_pass = $_POST['pass1'];
    
    $userdata = array(
    'user_login'    =>  $user_login ,
    'user_pass'=> $user_pass,
    'user_email'=>$user_email
    );
    
    $user_id = wp_insert_user( $userdata ) ;
    
             wp_update_user(
                array(
                  'ID'          =>    $user_id,
                  'nickname'    =>    $email_address
                )
              );
              $user = new WP_User( $user_id );
              $user->set_role( 'contributor' );
            }
    
    
        }else{
            echo "Sorry, your passwords do not match <br/>";
        }
    }else{
        echo "Sorry your Emails dont match <br/>";
        exit();
    }
    
    }else{
    $form = <<<EOT
    <form action="" method="POST">
        First Name: <input type="text" name="name"/> <br/>
        Last Name:  <input type="text" name="lname"/> <br/>
        Username:   <input type="text" name="uname"/> <br/>
        Email:  <input type="text" nam="email1"/> <br/>
        Confim Email:   <input type="text" name="email2"/> <br/>
        Password:   <input type="password" name="pass1"/> <br/>
        Confirm Password:   <input type="password" name="pass2"/> <br/>
        <input type="submit" value="Register" name="submit"/>
    </form>
    EOT;
    
    echo $form;
    }
    
    ?>
    
    <?php get_footer(); ?>
    

    This might work for you !! Good luck

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同