Skip to content

Instantly share code, notes, and snippets.

@avastamin
Last active December 4, 2016 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avastamin/b49481968fd5f984c1e9bd51f91779b4 to your computer and use it in GitHub Desktop.
Save avastamin/b49481968fd5f984c1e9bd51f91779b4 to your computer and use it in GitHub Desktop.
/*
* Function to create form shortcode
*/
function request_form_func($atts, $content = null){
extract(shortcode_atts(array(
'id' => false,
'class' => false,
'title' => false,
'subtitle' => false,
'action' => "/thanks",
'key' => false,
'button_text' => "Submit"
), $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="name@domain.com" 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 .= '<input type="hidden" id="hash" name="hash" value="'.$email_hash.'" >';
}
$out .= '<div class="field-wrap submit-wrap"><input type="submit" value="' .$button_text. '" name="submit"></div>';
$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 shortcode to take care data
*/
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:" . "admin@domain.com";
$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($db_hash)){
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', 'thanks_func');
/*
* Function to create new record in specified table
*/
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 to update new record in specified table
*/
function update_custom_user($username, $password, $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->update( $table_name, array(
'username' => $username,
'password' => $password,
'updated_at' => $cur_date
),
array(
"hash" => $hash
) );
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment