Skip to content

Instantly share code, notes, and snippets.

@BenRacicot
Last active August 29, 2015 14:22
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 BenRacicot/9ddfd5c0ab7f1aef01f2 to your computer and use it in GitHub Desktop.
Save BenRacicot/9ddfd5c0ab7f1aef01f2 to your computer and use it in GitHub Desktop.
Extending WordPress Plugin DM Confirm Email
add_action( 'init', 'my_ajax_init' );
function my_ajax_init() {
add_action('wp_ajax_nopriv_create_user', 'create_user_function');
add_action('wp_ajax_create_user', 'create_user_function');
}
/* ------------------------------------------------------------------------ *
Checks if email & username are in use
Creates user with DmConfirmEmail_Models_Registration
* ------------------------------------------------------------------------ */
function create_user_function(){
//check_ajax_referer( 'sk-ajax-nonce', 'nonce'); // look into setting up a nonce for security
$array = array();
$email = isset($_POST['email']) ? $_POST['email'] : 'null';
$login = isset($_POST['uname']) ? $_POST['uname'] : 'null';
// check if username exists in the WP database
if ( username_exists( $login ) )
{
$array['wpuser'] = true;
$array['status'] = "Looks like $login is taken!";
echo json_encode($array);
die();
}
else if ( email_exists( $email ) )
{
$array['wpemail'] = true;
echo json_encode($array);
die();
}
// check if username / email exists in the DM Confirm table
$emailExists = checkExist( 'user_email', $email );
$loginExists = checkExist( 'user_login', $login );
if ( $loginExists != 0 ){
$array['dmlogin'] = true;
$array['status'] = "Looks like youve signed up before with $login <br />Try a password recovery.";
echo json_encode($array);
die();
}
else if( $emailExists != 0 )
{
$array['dmemail'] = true;
$array['status'] = "Looks like youve signed up before with $email <br />You should have a confirmation email.";
echo json_encode($array);
die();
}
else
{
// call DM Confirm plugin function to create user
$DM = new DmConfirmEmail_Models_Registration($login, $email);
// not sure how to get any status from $DM->register(); @BenRacicot
$DM->register();
$array['status'] = 'Please check your email to confirm your new account - This is very exciting!';
echo json_encode($array);
}
die();
}
/**
* reuse the DM Confirm Plugin checkExist function to see if user has already tried to signup
* Checks if user exists in the DM Confirm unconfirmed usernames and emails table
* @param string $user Either user_login or user_email only
* @param string $data
* @return mixed
*/
function checkExist($user, $data) {
global $wpdb;
$result = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->prefix}" . DmConfirmEmail::PLUGIN_ALIAS . " WHERE {$user} = %s",
$data
)
);
return $result;
}
// This form is the CTA, for use anywhere.
// After inputting the email here the user is redirected to /signup/
// Once there, the fields will be have the email already filled out (see local storage in usercreate.js)
<div class="signupcta">
<a class="centeredTxt" href="/signup/">
<h3>Create an account<br />Cuz we're awesome!<br />Get Exclusive deals!</h3>
</a>
<input type='text' id='frontPageEmail' value='' maxlength='50' placeholder='email'>
</div>
<?php
/*
Template Name: Signup
*/
// If they enter their data here it is saved to localStorage. This way if they abandone the form when they come back
// it will be prefilled out with their info... Pretty cool eh?
?>
<?php get_header(); ?>
<div id="content">
<div id="inner-content" class="wrap clearfix">
<div id="main" class="first clearfix" role="main">
<div class="signin">
<?php if ( ! is_user_logged_in() ) { ?>
<h2>Create Account</h2>
<input name='' type='text' id='Email' value='' size='' maxlength='' placeholder='Email'>
<input name='' type='text' id='First' value='' size='' maxlength='' placeholder='First Name'>
<input name='' type='text' id='Last' value='' size='' maxlength='' placeholder='Last Name'>
<input name='' type='text' id='UName' value='' size='' maxlength='' placeholder='User Name'>
<input id='createAcc' class='' name='' type='button' value='Create Account'>
<?php } else {
//header('Location: '.home_url().'/profile/');
echo "<h3>You are already logged in.</h3>";
global $current_user;
get_currentuserinfo();
}
// hook failed login
add_action('wp_login_failed', 'my_front_end_login_fail');
?>
<div id="successMsg"></div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
/* ------------------------------------------------------------------------ *
HOME PAGE SIGNUP FORM
* ------------------------------------------------------------------------ */
// save user data as they type to make it that much easier for them on the signup page
jQuery("#frontPageEmail").on('keyup', function(e){
var email = jQuery("#frontPageEmail").val();
if ( this.value.length > 8 )
{
localStorage.setItem('Email', email);
}
if ( e.which == 13 && this.value.length > 8 )
{
window.location.assign('/signup/'); // /create-account/
}
});
/* ------------------------------------------------------------------------ *
GET YA FORM VALUES HE'AH
* ------------------------------------------------------------------------ */
// If information has been previously entered propagate it
jQuery("#Email").val(localStorage.Email);
jQuery("#First").val(localStorage.First);
jQuery("#Last").val(localStorage.Last);
jQuery("#UName").val(localStorage.UName);
// save value to prospective localStorage (blur for autocomplete values)
jQuery("input").on('blur keyup', function(e){
var field = jQuery(this).attr('id');
localStorage.setItem(field, this.value);
});
// On Submission
jQuery('#createAcc').on('click', function(){
// Validation
if ( jQuery("#Email").val().length > 8 && jQuery("#UName").val().length > 2 ){
console.log('Submitted!');
var data = {
action: 'create_user',
//nonce: skNonce, // look into setting up a nonce for security
email: localStorage.Email,
first: localStorage.First,
last: localStorage.Last,
uname: localStorage.UName
}
createUser(data);
} else { console.log('Email issues'); }
});
function createUser(data){
jQuery.ajax({
type:"POST",
url: My_Obj.ajaxurl, // the request is sent to this url
data: data,
dataType: 'json',
beforeSend: function () { /* AJAX request is about to be sent don't forget about async: true */ },
complete: function () { /* AJAX request has completed */ },
success: function (response) {
console.log('success');
console.log('object: ' + JSON.stringify(response, null, 4));
jQuery("#successMsg").text(console.log('status' + response.status) );
},
error: function (xhr, status, error) {
console.log( 'xhr: ' + JSON.stringify(xhr.responseJSON, null, 4) );
console.log( 'status: '+ status );
console.log( 'error: '+ error );
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment