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 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle