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.
<?php echo $_POST['first_name']; ?>
etc. alsoaction
is<?php echo $_SERVER['REQUEST_URI']; ?>
, so it is posting to itself basically.