Skip to content

Instantly share code, notes, and snippets.

@MrImranJaved
Forked from vishalbasnet23/functions.php
Created June 9, 2022 20:29
Show Gist options
  • Save MrImranJaved/ed708bd020450a268126748f67b4315c to your computer and use it in GitHub Desktop.
Save MrImranJaved/ed708bd020450a268126748f67b4315c 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment