1

I have a 2 step website registration for my WordPress website.

BEGIN EDIT

The first step/form is a front end verification form plugin I have built that checks a custom_table to verify the visitor is a part of the custom_table.

The second step/form is a frontend registration form plugin I have built which I have used the same "name" on 3 of the fields as in the first form as well as echoed the first forms input fields in the value of the second forms input fields.

END EDIT

What I am trying to do is have the form data from 3 fields in the first form populate the 3 fields in the second form.

I tried adding the second url to the action area of the form and matched up the names of the form which did successfully populate the data to the second form however when doing this my validation stopped working on the first form. As such I have scrapped that idea.

I have now also edited the code to reflect the changes I have made with hwl's comments below and would love further insight as now I am seeing the validation script is working but when you enter the successful information the form is still not loading the information on the second page. But no Fatal Errors either so that's a good thing lol.

BEGIN EDIT - DISPLAY BOTH FULL FORMS

FORM 1 - Verification Plugin

 <?php
 /*
  * Plugin Name: Custom Member Verification
  * Plugin URI: http://digitaldesigneronline.com
  * Description: Adds a Custom Member Verification Form to your WordPress website with a simple shortcode. Use the shortcode [cv] to add the verification form to a page, post or widget area.
  * Version: 1.0
  * Author: Chad Warford - The Online Lifesaver
  * Author URI: http://digitaldesigneronline.com
  */

 function cv(&$fields, &$errors) {

   // Check args and replace if necessary
   if (!is_array($fields))     $fields = array();
   if (!is_wp_error($errors))  $errors = new WP_Error;

   // Check for form submit
   if (isset($_POST['submit'])) {

     // Get fields from submitted form
     $fields = cv_get_fields();

     // Validate fields and produce errors
     if (cv_validate($fields, $errors)) {

    // If successful, display a message

      // Clear field data
      // $fields = array(); 
     }
   }

   // Santitize fields
   cv_sanitize($fields);

   // Generate form
   cv_display_form($fields, $errors);
 }

 function cv_sanitize(&$fields) {
   $fields['user_login']   =  isset($fields['user_login'])  ? sanitize_user($fields['user_login']) : '';
   $fields['first_name']   =  isset($fields['first_name'])  ? sanitize_text_field($fields['first_name']) : '';
   $fields['last_name']    =  isset($fields['last_name'])   ? sanitize_text_field($fields['last_name']) : '';
   $fields['SinLast4']    =  isset($fields['SinLast4'])   ? esc_attr($fields['SinLast4']) : '';
 }

 function cv_display_form($fields = array(), $errors = null) {

   // Check for wp error obj and see if it has any errors  
   if (is_wp_error($errors) && count($errors->get_error_messages()) > 0) {

     // Display errors
     ?>
    <div style="display:block;margin:auto;text-align:left;max-width:1080px;">
    <ul><?php
     foreach ($errors->get_error_messages() as $key => $val) {
       ?><li>
         <?php echo $val; ?>
       </li><?php
     }
     ?></ul><?php
   }

   // Display form
   ?>
   </div>  
   <div style="display:block;margin:auto;text-align:left;max-width:1080px;">

   <form action="<?php $_SERVER['REQUEST_URI'] ?>" method="post">
     <div>
        <label for="user_login">Card Number <strong>*</strong></label>
        <input type="text" name="user_login" id="user_login" value="<?php echo (isset($fields['user_login']) ? $fields['user_login'] : '') ?>" >
     </div>
    <br>
    <div>
       <label for="first_name">First Name <strong>*</strong></label>
       <input type="text" name="first_name" id="first_name" value="<?php echo (isset($fields['first_name']) ? $fields['first_name'] : '') ?>" >
     </div>
    <br>
     <div>
       <label for="last_name">Last Name <strong>*</strong></label>
       <input type="text" name="last_name" id="last_name" value="<?php echo (isset($fields['last_name']) ? $fields['last_name'] : '') ?>" >
     </div>
    <br>
     <div>
       <label for="SinLast4">Last 4 digits of SIN:<strong>*</strong></label>
       <input type="text" name="SinLast4" id="SinLast4" value="<?php echo (isset($fields['SinLast4']) ? $fields['SinLast4'] : '') ?>">
     </div>
    <br>
     <br>
     <input type="submit" name="submit" value="Verify Membership">
     </form>
    <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
        <input type="hidden" name="user_login" value="<?php echo $_POST['user_login']; ?>">
        <input type="hidden" name="first_name" value="<?php echo $_POST['first_name']; ?>">
        <input type="hidden" name="last_name" value="<?php echo $_POST['last_name']; ?>">
    </form>
 </div>
 <?php
 }

 function cv_get_fields() {
   return array(
     'user_login'   =>  isset($_POST['user_login'])   ?  $_POST['user_login']   :  '',
     'first_name'   =>  isset($_POST['first_name'])   ?  $_POST['first_name']        :  '',
     'last_name'    =>  isset($_POST['last_name'])    ?  $_POST['last_name']        :  '',
     'SinLast4'   =>  isset($_POST['SinLast4'])   ?  $_POST['SinLast4']        :  '',
   );
 }

 function cv_validate(&$fields, &$errors) {

   // Make sure there is a proper wp error obj
   // If not, make one
   if (!is_wp_error($errors))  $errors = new WP_Error;

   // Validate form data

   if (empty($fields['user_login']) || empty($fields['first_name']) || empty($fields['last_name']) || empty($fields['SinLast4'])) {
     $errors->add('field', 'Please remember to enter your information exactly as it appears on your Dues Receipt.');
   }

   if (strlen($fields['user_login']) < 7) {
   $errors->add('username_length', 'Card Number too short. Pleas verify you have entered the entire Card Number');
   }

     if (strlen($fields['SinLast4']) < 4) {
   $errors->add('sin_length', 'Please verify you have entered only the last 4 digits of your SIN.');
   }
   // If errors were produced, fail
   if (count($errors->get_error_messages()) > 0) {
     return false;
   }

   // Define $Card $First $Last $SinLast4
        global $wpdb;
        $Card=$_POST['user_login'];
        $First=$_POST['first_name'];
        $Last=$_POST['last_name'];
        $SinLast4=$_POST['SinLast4'];
           $result = $wpdb->get_results( "SELECT * FROM custom_table WHERE Card = '$Card' AND First = '$First' AND Last = '$Last' AND SinLast4 = '$SinLast4'");
          // $query = "SELECT * FROM custom_table WHERE Card = $Card AND First = $First AND Last = $Last AND SinLast4 = $SinLast4";
          // $result = $wpdb->get_results($query);
        foreach ( $result as $query )   {
        echo "<script>window.location='http://localhost/dev1/member-registration/';</script>";  // Update URL to registration page
        header("location: http://localhost/dev1/member-registration/"); // Redirecting To Other Page
        } 
   // Else, success!
   return true;
 }


 // The callback function for the [cv] shortcode
 function cv_cb() {
   $fields = array();
   $errors = new WP_Error();

   // Buffer output
   ob_start();

   // Custom verification, go!
   cv($fields, $errors);

   // Return buffer
   return ob_get_clean();
 }
 add_shortcode('cv', 'cv_cb');

FORM 2 - Registration Plugin

 <?php

 /*
  * Plugin Name: Custom Registration
  * Description: Adds a Custom Registration Form to your WordPress website with a simple shortcode. Use the shortcode [cr] to add the registration form to a page, post or widget area.
  * Version: 1.0
  */

 function cr(&$fields, &$errors) {

   // Check args and replace if necessary
   if (!is_array($fields))     $fields = array();
   if (!is_wp_error($errors))  $errors = new WP_Error;

   // Check for form submit
   if (isset($_POST['submit'])) {

     // Get fields from submitted form
     $fields = cr_get_fields();

     // Validate fields and produce errors
     if (cr_validate($fields, $errors)) {

    // If successful, register user
       $user_id = wp_insert_user($fields);

      // update user metadata
       update_user_meta( $user_id, 'wpcf-user-join-date', time()); /////insert account creation date
       // Send the new user the welcome email
       wp_new_user_notification($user_id,$fields['user_pass']);

       // And display a message
       echo nl2br ("Your account has been created and an activation link has been sent to the email address you entered.\n Note that you must activate the account by clicking on the activation link when you get the email before you can login.");

       // Clear field data
       $fields = array(); 
     }
   }

   // Santitize fields
   cr_sanitize($fields);

   // Generate form
   cr_display_form($fields, $errors);
 }

 function cr_sanitize(&$fields) {
   $fields['user_login']   =  isset($fields['user_login'])  ? sanitize_user($fields['user_login']) : '';
   $fields['first_name']   =  isset($fields['first_name'])  ? sanitize_text_field($fields['first_name']) : '';
   $fields['last_name']    =  isset($fields['last_name'])   ? sanitize_text_field($fields['last_name']) : '';
   $fields['user_email']   =  isset($fields['user_email'])  ? sanitize_email($fields['user_email']) : '';
   $fields['email_confirm']   =  isset($fields['email_confirm'])  ? sanitize_email($fields['email_confirm']) : '';
   $fields['user_pass']    =  isset($fields['user_pass'])   ? esc_attr($fields['user_pass']) : '';
   $fields['user_pass_retyped']    =  isset($fields['user_pass_retyped'])   ? esc_attr($fields['user_pass_retyped']) : '';
 }

 function cr_display_form($fields = array(), $errors = null) {

   // Check for wp error obj and see if it has any errors  
   if (is_wp_error($errors) && count($errors->get_error_messages()) > 0) {

     // Display errors
     ?>
    <div style="display:block;margin:auto;text-align:left;max-width:1080px;">
    <ul><?php
     foreach ($errors->get_error_messages() as $key => $val) {
       ?><li>
         <?php echo $val; ?>
       </li><?php
     }
     ?></ul><?php
   }

   // Display form
   ?>
   </div>
   <?php wp_enqueue_script( 'password-strength-meter' ); ?>

   <div style="display:block;margin:auto;text-align:left;max-width:1080px;">

   <form action="<?php $_SERVER['REQUEST_URI'] ?>" method="post">
     <div>
        <label for="user_login">Card Number <strong>*</strong></label>
        <input type="text" name="user_login" value="<?php echo $_POST['user_login']; ?><?php echo (isset($fields['user_login']) ? $fields['user_login'] : '') ?>" >
     </div>
    <br>
    <div>
       <label for="first_name">First Name <strong>*</strong></label>
       <input type="text" name="first_name" value="<?php echo $_POST['first_name']; ?><?php echo (isset($fields['first_name']) ? $fields['first_name'] : '') ?>" >
     </div>
    <br>
     <div>
       <label for="last_name">Last Name <strong>*</strong></label>
       <input type="text" name="last_name" value="<?php echo $_POST['last_name']; ?><?php echo (isset($fields['last_name']) ? $fields['last_name'] : '') ?>" >
     </div>
    <br>
     <div>
       <label for="email">Email <strong>*</strong></label>
       <input type="text" name="user_email" value="<?php echo (isset($fields['user_email']) ? $fields['user_email'] : '') ?>">
     </div>
    <br>
     <div>
       <label for="confirm-email">Confirm Email <strong>*</strong></label>
       <input type="text" name="email_confirm" value="<?php echo (isset($fields['email_confirm']) ? $fields['email_confirm'] : '') ?>">
     </div>
    <br>
     <div>
       <label for="user_pass">Password <strong>*</strong></label>
       <input type="password" name="user_pass">
    </div>
    <div>
      <label for="user_pass_retyped">Re-Enter Password <strong>*</strong></label>
      <input type="password" name="user_pass_retyped">
    </div>
    <div>
      <span id="password-strength"></span>
     </div>
     <br>
     <input type="submit" name="submit" value="Register">
     </form>
    </div>
 <?php
 }

 function cr_get_fields() {
   return array(
     'user_login'   =>  isset($_POST['user_login'])   ?  $_POST['user_login']   :  '',
     'first_name'   =>  isset($_POST['first_name'])   ?  $_POST['first_name']        :  '',
     'last_name'    =>  isset($_POST['last_name'])    ?  $_POST['last_name']        :  '',
     'user_email'   =>  isset($_POST['user_email'])   ?  $_POST['user_email']        :  '',
     'email_confirm'   =>  isset($_POST['email_confirm'])   ?  $_POST['email_confirm']        :  '',
     'user_pass'    =>  isset($_POST['user_pass'])    ?  $_POST['user_pass']    :  '',
     'user_pass_retyped'    =>  isset($_POST['user_pass_retyped'])    ?  $_POST['user_pass_retyped']    :  ''
   );
 }

 function cr_validate(&$fields, &$errors) {

   // Make sure there is a proper wp error obj
   // If not, make one
   if (!is_wp_error($errors))  $errors = new WP_Error;

   // Validate form data

   if (empty($fields['user_login']) || empty($fields['first_name']) || empty($fields['last_name']) || empty($fields['user_email']) || empty($fields['email_confirm']) || empty($fields['user_pass']) || empty($fields['user_pass_retyped'])) {
     $errors->add('field', 'Please complete the registration form.');
   }

   if (($fields['user_pass'].value) != ($fields['user_pass_retyped'].value))
     {
    $errors->add('user_pass_retyped', 'Those passwords don\'t match!');
     }

   // if (strlen($fields['user_login']) < 8) {
   //  $errors->add('username_length', 'Card Number too short. Pleas verify you have entered the entire Card Number');
   // }

   if (username_exists($fields['user_login']))
     $errors->add('user_name', 'Sorry, that Card Number has already been registered.');

   if (strlen($fields['user_pass']) < 8) {
     $errors->add('user_pass', 'Password length must be greater than 8');
   }

   if (!is_email($fields['user_email'])) {
     $errors->add('email_invalid', 'Please verify the email address prior to submitting.');
   }

   if (!is_email($fields['email_confirm'])) {
     $errors->add('email_confirm_invalid', 'Please verify the email address prior to submitting.');
   }

   if (($fields['user_email'].value) != ($fields['email_confirm'].value))
     {
     $errors->add('email_mismatch', 'Those emails don\'t match!');
     }

   if (email_exists($fields['user_email'])) {
     $errors->add('email_taken', 'Please use another email address as it appears that one is already in use.');
   }

   // If errors were produced, fail
   if (count($errors->get_error_messages()) > 0) {
     return false;
   }

   // Else, success!
   return true;
 }

 // Redefine user notification function
 if ( !function_exists('wp_new_user_notification') ) {
     function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {
        $user = new WP_User($user_id);
        $hash = md5( $random_number );
        add_user_meta( $user_id, 'hash', $hash );
        $user_login = stripslashes($user->user_login);
        $first_name = stripslashes($user->first_name);
        $user_email = stripslashes($user->user_email);

         $message  = sprintf(__('New user registration on your blog %s:'), get_option('blogname')) . "\n";
         $message .= sprintf(__('Username: %s'), $user_login) . "\n";
         $message .= sprintf(__('First Name: %s'), $first_name) . "\n";
         $message .= sprintf(__('E-mail: %s'), $user_email) . "\n";

         @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);

         if ( empty($plaintext_pass) )
             return;

         $message  = sprintf(__('Hello %s,'), $first_name) . "\n";
         $message .= sprintf(__("Thank you for registering at %s."), get_option('blogname')) . "\n";
      // $message .= wp_login_url() . "\n";
         $message .= sprintf(__('Your account is created and must be activated before you can use it.')) . "\n";
        $message .= sprintf(__('To activate the account click on the following link or copy-paste it in your browser:')) . "\n";
        $message .= home_url('/').'activate?id='.$un.'&key='.$hash."\n";
        $message .= sprintf(__('After activation you may login to %s'), site_url()) . "\n";
         $message .= sprintf(__('Here are your Login Credentials:')) . "\n";
         $message .= sprintf(__('Username: %s'), $user_login) . "\n";
         $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\n";
         $message .= sprintf(__('If you have any problems, please contact me at %s.'), get_option('admin_email')) . "\n";
         $message .= sprintf(__('Best Regards, %s'), get_option('blogname'));

         wp_mail($user_email, sprintf(__('[%s] Member Registration Email Verification'), get_option('blogname')), $message);

     }
 }
 // The callback function for the [cr] shortcode
 function cr_cb() {
   $fields = array();
   $errors = new WP_Error();

   // Buffer output
   ob_start();

   // Custom registration, go!
   cr($fields, $errors);

   // Return buffer
   return ob_get_clean();
 }
 add_shortcode('cr', 'cr_cb');

Question How can I get the user_login, first_name & last_name entries from the first form to populate the second form without losing the step 1 validation code? Please provide an example if possible.

  • have you check that after completion of each steps, do it save to database or only update database after all the steps were being submited? If the database is updated after completion all the steps then you could do it using jQuery to pass values to and fro on different steps if not then you could check the database and get the required values and populate on the next steps – Aishan Jun 7 '17 at 6:23
  • Hi @Aishan, thanks for commenting. the first form simply checks the database to verify the content. The second form where I need the content from the first form to populate is the registration form which is populated from a shortcode within the Profile Builder Plugin – DigitalDesigner Jun 7 '17 at 7:26
  • a multi part / step form is more simple than you think, post it to itself passing the values needed in the final $_POST as hidden fields (pass nonces there too if needed). No idea on hooking the plugin b/c I'm not familiar with it (I'd need to see the code for that), but see if this answer gives you enough to work with: wordpress.stackexchange.com/questions/268315/… – hwl Jun 7 '17 at 23:06
  • Hi @hwl I have looked through the lesson you gave on the other question and though I thought I had it implimented correctly I may need further insight. I added the extra form and set the hidden fields but am not seeing the information populate to the next step. What did I do wrong? – DigitalDesigner Jun 8 '17 at 2:32
  • You need to echo it. <?php echo $_POST['first_name']; ?> etc. also action is <?php echo $_SERVER['REQUEST_URI']; ?>, so it is posting to itself basically. – hwl Jun 8 '17 at 3:43
0

I have been able to find a solution to my issue. Here is what I did to solve the issue. It was a bit of a walkaround and mostly analyzing at which point things went wrong.

Step 1. Established that the biggest issue I was having was moving the information from one page to another. So instead I have dropped both forms into the same page adding a div styling to display:none for the second form, hiding it from the browsers view.

Step 2. Now that I had both forms on the same page I edited the echo redirect to be the current page on a successful form submission on form 1.

Step 3. In order to hide the first form and reveal the second form I dropped in a style echo to the success area as well in order to display form 2 and another style echo to hide form 1.

To the visitor the forms produce seamlessly and all information from form one is correctly moving to form 2.

Here is my edited code.

Under Validate Fields and Produce errors

// Validate fields and produce errors
if (cv_validate($fields, $errors)) {

// If successful, display a message
        echo '<style type="text/css">.step1-form{display:none !important;}</style>';
        echo '<style type="text/css">.step2-form{display:block !important;}</style>';

}

This allows the first form to hide on success and the second form to reveal.

I also added the classes to the corresponding forms and div tags surrounding the forms and voila, the two plugins perfectly communicate with one another and the visitor does not have to re-enter their data on the second form making their registration process just a little easier.

I hope someone finds this helpful for their future projects. Thanks for the communities input on this topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.