步骤1
首先,您应该创建两个单独的模板(每个步骤一个).在第一个模板中,您应该创建一个表单,将用户电子邮件发送到第二页.该链接应具有GET属性,以便您可以获取他的电子邮件和名字.这是一个例子(注意它可以改进):
<?php
/*
** Template Name: Step 1
*/
get_header();
if ( !empty( $_POST['firstname'] ) && !empty( $_POST['email'] ) ) {
$link = 'http://my-site/step-2';
$link = add_query_arg(
array(
'firstname' => $_POST['firstname'],
'email' => $_POST['email'],
),
$link
);
$subject = 'New user registration';
$message = 'Please click on the following link to complete your registration: ' . $link;
$headers = array('Content-Type: text/html; charset=UTF-8');
$result = wp_mail( $_POST['email'], $subject, $message, $headers );
if ( $result ) {
$message = 'Please check your email address to complete the registration';
} else {
$message = 'Something went wrong. Please contact the administrator';
}
echo $message;
} else {
?>
<form method="POST" action="">
<input type="text" name="firstname" placeholder="First Name">
<input type="email" name="email" placeholder="Email address">
<input type="submit" value="Submit">
</form>
<?php
}
get_footer();
Run Code Online (Sandbox Code Playgroud)
如果提交表单并填写所有字段,我们会创建一个简单的检查.如果是这样,我们可以发送电子邮件到第2步.
第2步
我们将创建一个单独的模板,我们将使用第一个填充数据$_GET,并且我们将添加两个将为空的新字段(用户名和密码).
<?php
/*
** Template Name: Step 2
*/
get_header();
if ( !empty( $_POST['firstname'] ) && !empty( $_POST['email'] ) && !empty( $_POST['password'] ) ) {
$user_id = username_exists( $_POST['username'] );
if ( !$user_id and email_exists($_POST['email']) == false ) {
$user_id = wp_create_user( $_POST['username'], $_POST['password'], $_POST['email'] );
if ( $user_id ) {
update_user_meta($user_id, 'first_name', $_POST['firstname']);
$message = 'User has been created';
}
} else {
$message = 'User already exists!';
}
echo $message;
} else {
?>
<form method="POST" action="">
<input type="text" name="firstname" value="<?php echo ( !empty( $_GET['firstname'] ) ) ? $_GET['firstname'] : '' ; ?>" placeholder="First Name">
<input type="email" name="email" value="<?php echo ( !empty( $_GET['email'] ) ) ? $_GET['email'] : '' ; ?>" placeholder="Email Address">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<input type="submit" value="Submit">
</form>
<?php
}
get_footer();
Run Code Online (Sandbox Code Playgroud)
如果第二个表单已提交且一切正常,我们可以创建用户.创建后,我们可以更新其名字.
您可以对我的代码进行无限制的修改,但这是基础.例如,我们可以创建所需的字段,我们可以检查密码强度,名称长度等.