我正试图实现以下目标,并且我不知道从哪里开始。Wordpress两步注册表
我试图用两个步骤创建一个注册/注册表单。
第一步:是有2个输入字段(名称,电子邮件),当用户提交时,电子邮件与链接发送到第二步。
第二步:用户输入发送到他的电子邮件的链接,用第二个表单输入一个页面。具有他使用的电子邮件和名称的价值,+ 2其他字段(用户名,密码),他可以在其中访问特定页面。
我无法找到从哪里开始,也没有插件满足以下要求。
Regards,
Mostafa。
我正试图实现以下目标,并且我不知道从哪里开始。Wordpress两步注册表
我试图用两个步骤创建一个注册/注册表单。
第一步:是有2个输入字段(名称,电子邮件),当用户提交时,电子邮件与链接发送到第二步。
第二步:用户输入发送到他的电子邮件的链接,用第二个表单输入一个页面。具有他使用的电子邮件和名称的价值,+ 2其他字段(用户名,密码),他可以在其中访问特定页面。
我无法找到从哪里开始,也没有插件满足以下要求。
Regards,
Mostafa。
我想开发这样的事情,我只需要的方法(:谢谢你的插件将调查它 –
尝试this插件为您的目的。
以下是您可以采取的简短步骤。
确保用户是唯一的并存储用于确认的凭证。
if(!email_exists($email)){
/*
Store credentials in a custom table
with a unique identifier,
hashed password and email.
Email user with the confirmation link ex.
site.com/confirmation/<unique-identifier>
*/
}
在confirmation
页面通过简单地创建用户:
// Confirm <unique-identifier>
// Create user
$user_id = wp_create_user($email, $password, $email);
// Set user role
$user = new WP_User($user_id);
$user->set_role('contributor'); // or a custom role
感谢您的高抬头,我很乐意进一步解释请 –
@MostafaMohsen,详细解释我的答案:http://stackoverflow.com/a/40959220/1960558 –
尝试o创建一个自定义插件,并创建一个2个简码。
分别创建2个页面并插入2个简码。
在第一个短代码中,写一个表单的代码让用户输入他们的电子邮件地址和名称。
然后在wp_insert_user
的数据库中输入详细信息(姓名和电子邮件地址),然后发送电子邮件到电子邮件地址,生成第二页的链接并在链接中附加加密的用户ID。
当用户点击链接时,将他们重定向到第二页,在该页面中使用选择查询自动填充姓名和电子邮件地址。
在此页面中插入所有其他详细信息。
STEP 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();
如果我们提交表单,所有的领域都充满创建一个简单的检查。 如果这样我们就可以发送电子邮件到步骤2
STEP 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();
如果第二个表单被提交并且一切正常,我们可以创建用户。 一旦创建,我们可以更新它的名字。
您可以对我的代码进行无限制的修改,但这是基础。例如,我们可以把所需的字段,我们可以检查密码强度,名称长度等
我更喜欢使用简码。这是一步。完整的代码:
[request_form action="/thanks"]
注:内thanks_func
需要用户正确的URL $confirmation_body_text = "/signup/?hid='.$hash.'";
创建thanks
页面并添加此短代码:
[thanks] Thank you page [/thanks]
在用户数据库中创建新表格:
CREATE TABLE
wp_new_user (
ID int(11) NOT NULL,
名varchar(40) NOT NULL,
电子邮件varchar(80) NOT NULL,
用户名varchar(30) DEFAULT NULL,
密码varchar(30) DEFAULT NULL,
哈希varchar(40) DEFAULT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
的updated_at timestamp NULL DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
在第一步电子邮件将与链接,以便在第2步发送类似signup?hid=1679091c5a880faf6fb5e6087eb1b2dc
我们将从此独特的电子邮件散列中获取现有用户记录,并预先填充name
和email
字段。
第二步,我们只更新现有的用户记录。
功能request_form_func($的ATT,$含量= NULL){ 提取物(shortcode_atts(阵列( 'ID'=>假, '类'=>假, '标题'=>假, “字幕'=> false, 'action'=>“/ thanks”, 'key'=> false, 'button_text'=>“提交” ),$ atts));
if($class) { $class = ' ' . $class; }
else { $class = ''; }
if(empty($_GET)){
$next_step = false;
$db_name = '';
$db_email = '';
}
if(isset($_GET['hid'])){
$email_hash = trim($_GET['hid']);
$table = "wp_new_user";
$project_data = new wpdb('root','root','wordpress','localhost'); // Testserver
$rows = $project_data->get_results($project_data->prepare(
"
SELECT id,name, email, hash FROM " . $table . "
WHERE hash = %d
",
$email_hash
));
$db_hash = $rows[0]->hash;
if($db_hash == $email_hash) {
$field_id = $rows[0]->id;
$db_name = $rows[0]->name;
$db_email = $rows[0]->email;
$next_step = true;
}
}
$out = '';
if($id) { $id = '-'.$id; }
$out .= '<div class="request_form'.$class.'">';
$out .= '<div class="form-wrap">';
if($title) {
$out .= '<span class="title">' . $title . '</span>';
}
$out .= '<form id="step-form" class="cf" method="post" action="'.$action.'">';
$out .= '<div class="field-wrap"><label for="fullname'.$id.'"><span class="desc">Name</span>';
$out .= '<input type="text" id="fullname'.$id.'" name="fullname" data-required="true" placeholder="Jon Doe" value="'.$db_name.'"></label></div>';
$out .= '<div class="field-wrap"><label for="email'.$id.'"><span class="desc">E-Mail</span>';
$out .= '<input type="email" id="email'.$id.'" name="email" data-required="true" placeholder="[email protected]" value="'.$db_email.'"></label></div>';
if($next_step){
$out .= '<div class="field-wrap"><label for="username'.$id.'"><span class="desc">Username</span>';
$out .= '<input type="text" id="username'.$id.'" name="username" data-required="true" placeholder="username"></label></div>';
$out .= '<div class="field-wrap"><label for="password'.$id.'"><span class="desc">Password</span>';
$out .= '<input type="password" id="password'.$id.'" name="password" data-required="true" placeholder="password"></label></div>';
$ out。=''; } $ out。='';
$out .= wp_nonce_field('step_form', 'step_form_nonce'.$id, true, false);
$out .= '</form>';
$out .= '</div>';
$out .= '</div>';
return $out;
} add_shortcode( 'request_form', 'request_form_func');
然后我创建了感谢短代码thanks
这将照顾您的表单数据。基本上,第一步你需要保存你的数据,你也需要生成唯一的ID通过电子邮件发送并保存到数据库。链接看起来像'注册?hid = 1679091c5a880faf6fb5e6087eb1b2dc'
function thanks_func($ atts,$ content = null){ $ out ='';
if(!empty($_POST) || wp_verify_nonce($_POST['step_form_nonce'],'step_form')){
}
else {
$out .= '<div class="content-area">';
$out .= '<h2 class="h1 page-title">Something went wrong</h2>';
$out .= '<p class="block">Please Re-Submit your form again</p>';
$out .= '</div>';
return $out;
}
if(isset($_POST['fullname'])){ $fullname = trim($_POST['fullname']); }
if(isset($_POST['email'])){ $email = trim($_POST['email']); }
if(isset($_POST['username'])){ $username = trim($_POST['username']); }
if(isset($_POST['password'])){ $password = trim($_POST['password']); }
if(isset($_POST['hash'])){ $db_hash = trim($_POST['hash']); }
$hash = md5(rand(0,1000)); // Generate random 32 character hash and assign it to a local variable.
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=utf-8\n";
$header .= "From:" . "[email protected]";
$confirmation_text = "Thanks for Submitting your first form";
$confirmation_body_text = "/registration/?hid='.$hash.'";
$subject = "Please click the link below";
$message = "Name: $fullname\n";
$message .= "Email Address: $email\n";
$message .= "Click the link: $confirmation_body_text\n";
if (!empty($username) && !empty($password) && !empty($field_id)){
update_custom_user($username, $password, $$db_hash);
} else if (create_custom_user($fullname, $email, $hash) && wp_mail($email, $subject, $message, $header)){
}
$out .= '<div class="content-area">';
$content = do_shortcode($content);
$out .= $content;
$out .= '</div>';
return $out;
}
add_shortcode( '感谢', 'thanks_func');
我也写2功能create_custom_user
和update_custom_user
将从第一步骤保存的数据的数据和在第二步骤更新username
和password
。
function create_custom_user($fullname, $email, $hash){
global $wpdb;
$table_name = $wpdb->prefix . "new_user";
$cur_date = new DateTime();
$cur_date->setTimezone(new DateTimeZone('Europe/Berlin'));
$cur_date = $cur_date->format('d.m.Y').', '.$cur_date->format('G:i');
$wpdb->insert($table_name, array(
'name' => $fullname,
'email' => $email,
'hash' => $hash,
'created_at' => $cur_date
));
return true;
}
function update_custom_user($username, $password, $field_id){
global $wpdb;
$table_name = $wpdb->prefix . "new_user";
$cur_date = new DateTime();
$cur_date->setTimezone(new DateTimeZone('Europe/Berlin'));
$cur_date = $cur_date->format('d.m.Y').', '.$cur_date->format('G:i');
$wpdb->update($table_name, array(
'username' => $username,
'password' => $password,
'updated_at' => $cur_date
),
array(
"id" => $field_id
));
return true;
}
请看看我的回答http://stackoverflow.com/a/40959220/1960558。它真的很长,但我希望你可以很容易地理解gist的代码:https://gist.github.com/avastamin/b49481968fd5f984c1e9bd51f91779b4 –