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 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制