Skip to content

Instantly share code, notes, and snippets.

@vishalbasnet23
Created October 31, 2014 08:57
Show Gist options
  • Save vishalbasnet23/1937b45be0ea73784cc5 to your computer and use it in GitHub Desktop.
Save vishalbasnet23/1937b45be0ea73784cc5 to your computer and use it in GitHub Desktop.
User Registration Front End WordPress with Ajax
<?php
add_action('wp_ajax_register_user_front_end', 'register_user_front_end', 0);
add_action('wp_ajax_nopriv_register_user_front_end', 'register_user_front_end');
function register_user_front_end() {
$new_user_name = stripcslashes($_POST['new_user_name']);
$new_user_email = stripcslashes($_POST['new_user_email']);
$new_user_password = $_POST['new_user_password'];
$user_nice_name = strtolower($_POST['new_user_email']);
$user_data = array(
'user_login' => $new_user_name,
'user_email' => $new_user_email,
'user_pass' => $new_user_password,
'user_nicename' => $user_nice_name,
'display_name' => $new_user_first_name,
'role' => 'subscriber'
);
$user_id = wp_insert_user($user_data);
if (!is_wp_error($user_id)) {
echo 'we have Created an account for you.';
} else {
if (isset($user_id->errors['empty_user_login'])) {
$notice_key = 'User Name and Email are mandatory';
echo $notice_key;
} elseif (isset($user_id->errors['existing_user_login'])) {
echo'User name already exixts.';
} else {
echo'Error Occured please fill up the sign up form carefully.';
}
}
die;
}
<p class="register-message" style="display:none"></p>
<form action="#" method="POST" name="register-form" class="register-form">
<fieldset>
<label><i class="fa fa-file-text-o"></i> Register Form</label>
<input type="text" name="new_user_name" placeholder="Username" id="new-username">
<input type="email" name="new_user_email" placeholder="Email address" id="new-useremail">
<input type="password" name="new_user_password" placeholder="Password" id="new-userpassword">
<input type="password" name="re-pwd" placeholder="Re-enter Password" id="re-pwd">
<input type="submit" class="button" id="register-button" value="Register" >
</fieldset>
</form>
<script type="text/javascript">
jQuery('#register-button').on('click',function(e){
e.preventDefault();
var newUserName = jQuery('#new-username').val();
var newUserEmail = jQuery('#new-useremail').val();
var newUserPassword = jQuery('#new-userpassword').val();
jQuery.ajax({
type:"POST",
url:"<?php echo admin_url('admin-ajax.php'); ?>",
data: {
action: "register_user_front_end",
new_user_name : newUserName,
new_user_email : newUserEmail,
new_user_password : newUserPassword
},
success: function(results){
console.log(results);
jQuery('.register-message').text(results).show();
},
error: function(results) {
}
});
});
</script>
@vishalbasnet23
Copy link
Author

vishalbasnet23 commented Aug 11, 2022

Hi,
Yes, I just noticed that you can just write something like the following for validation right after line number 20:
if ( newUserEmail.length <= 0 ) { console.log("some Validation Message") jQuery('.register-message').text("Email is required").show(); return; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment