gpt4 book ai didi

php - 无法在 WordPress 环境中以编程方式创建用户

转载 作者:行者123 更新时间:2023-11-29 07:59:23 24 4
gpt4 key购买 nike

我是 WordPress 新手,我期待通过前端注册页面添加用户,我现在真的很困惑,因为我无法让这段代码工作,它只是不会创建用户。

这是代码:

<?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(); ?>

谢谢。

不确定是否重要,但我选择了 wpwpp_ 作为前缀

最佳答案

检查你的函数 if( null == username_exists( $email_address ) )//$email_addrress 应该是 $email1 或 $email2

但我会推荐 wp_insert_user 而不是 wp_create_user ..

<?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(); ?>

这可能对你有用!祝你好运

关于php - 无法在 WordPress 环境中以编程方式创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24288342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com