2016-11-20 267 views
9

我正試圖實現以下目標,並且我不知道從哪裏開始。Wordpress兩步註冊表

我試圖用兩個步驟創建一個註冊/註冊表單。

第一步:是有2個輸入字段(名稱,電子郵件),當用戶提交時,電子郵件與鏈接發送到第二步。

第二步:用戶輸入發送到他的電子郵件的鏈接,用第二個表單輸入一個頁面。具有他使用的電子郵件和名稱的價值,+ 2其他字段(用戶名,密碼),他可以在其中訪問特定頁面。

我無法找到從哪裏開始,也沒有插件滿足以下要求。

Regards,

Mostafa。

+0

請看看我的回答http://stackoverflow.com/a/40959220/1960558。它真的很長,但我希望你可以很容易地理解gist的代碼:https://gist.github.com/avastamin/b49481968fd5f984c1e9bd51f91779b4 –

回答

1

我覺得現在你可以到這裏試試這個Contact form 7 Multi-Step Form插件:

Contact Form 7 Multi-Step Forms

試用演示here

以後可以瞭解如何開發這種登記表你的天賦

+0

我想開發這樣的事情,我只需要的方法(:謝謝你的插件將調查它 –

1

嘗試this插件爲您的目的。

3

以下是您可以採取的簡短步驟。

確保用戶是唯一的並存儲用於確認的憑證。

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 
+0

感謝您的高擡頭,我很樂意進一步解釋請 –

+0

@MostafaMohsen,詳細解釋我的答案:http://stackoverflow.com/a/40959220/1960558 –

1
  1. 嘗試o創建一個自定義插件,並創建一個2個簡碼。

  2. 分別創建2個頁面並插入2個簡碼。

  3. 在第一個短代碼中,寫一個表單的代碼讓用戶輸入他們的電子郵件地址和名稱。

  4. 然後在wp_insert_user的數據庫中輸入詳細信息(姓名和電子郵件地址),然後發送電子郵件到電子郵件地址,生成第二頁的鏈接並在鏈接中附加加密的用戶ID。

  5. 當用戶點擊鏈接時,將他們重定向到第二頁,在該頁面中使用選擇查詢自動填充姓名和電子郵件地址。

  6. 在此頁面中插入所有其他詳細信息。

6

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

如果第二個表單被提交併且一切正常,我們可以創建用戶。 一旦創建,我們可以更新它的名字。

您可以對我的代碼進行無限制的修改,但這是基礎。例如,我們可以把所需的字段,我們可以檢查密碼強度,名稱長度等

-1

我更喜歡使用簡碼。這是一步。完整的代碼:

  1. 創建一個頁面(註冊)。例如[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

    我們將從此獨特的電子郵件散列中獲取現有用戶記錄,並預先填充nameemail字段。

    1. 第二步,我們只更新現有的用戶記錄。

      功能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');

    2. 然後我創建了感謝短代碼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_userupdate_custom_user將從第一步驟保存的數據的數據和在第二步驟更新usernamepassword

    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; 
    }