add user with ajax by submiting form

<form id="ajaxform" action="ajaxform" method="post">
	<p class="status"></p>
    <label for="uname">User name:</label>
    <input type="text" name="uname" id="uname" required>
    <label for="uemail">User email:</label>
    <input type="email" name="uemail"id="uemail" required>
    <input type="submit" value="Login" name="submit">
</form>

.js file

jQuery(document).ready(function($) {
// Perform AJAX login on form submit
	$('form#ajaxform').on('submit', function(e){
	$('form#ajaxform p.status').show().text(ajax_signup_object.loadingmessage);
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: ajax_signup_object.ajaxurl,
			data: {
			'action': 'signup', //calls wp_ajax_nopriv_ajaxsignup
			'uname': $('form#ajaxform #uname').val(),
			'uemail': $('form#ajaxform #uemail').val(),
			},
			success: function(data) {
			   
			}
		});
	e.preventDefault();
	});
});

functions.php

/* Enque script for ajax */
function prefix_ajax_signup() {
wp_register_script('ajax-signup-script', get_template_directory_uri() . '/js/deafult.js', array('jquery'));
wp_enqueue_script('ajax-signup-script');
wp_localize_script('ajax-signup-script', 'ajax_signup_object', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'loadingmessage' => __('User created successfully')
));
}
add_action('wp_enqueue_scripts', 'prefix_ajax_signup');
/* End Enque script for ajax */

/* Insert user using ajax */
function insert_user() {
$uname = $_POST['uname'];
$uemail = $_POST['uemail'];
$userdata = array(
'user_login' => $uname,
'user_email' => $uemail,
);
$user_id = wp_insert_user($userdata);
}
add_action( 'wp_ajax_signup', 'insert_user' );
add_action( 'wp_ajax_nopriv_signup', 'insert_user' );
Advertisements

Tagged: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: