Tag Archives: user

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

display menu item only for registered user

functions.php

add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li><a href="'.get_permalink(74939).'">Documents</a></li>';
}
return $items;
}

Add User with Usermeta using ajax

&lt;/pre&gt;
&lt;form id=&quot;ajaxform_member&quot; action=&quot;ajaxform_member&quot; method=&quot;post&quot;&gt;
&lt;p class=&quot;status&quot;&gt;&lt;/p&gt;
&lt;label for=&quot;mname&quot;&gt;Name:&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;mname&quot; id=&quot;mname&quot; required&gt;&lt;/br&gt;

&lt;label for=&quot;maddress&quot;&gt;Address:&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;maddress&quot; id=&quot;maddress&quot; required&gt;&lt;/br&gt;

&lt;label for=&quot;mphone_num&quot;&gt;Phone Number:&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;mphone_num&quot; id=&quot;mphone_num&quot; required&gt;&lt;/br&gt;

&lt;label for=&quot;memail&quot;&gt;Email:&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;memail&quot; id=&quot;memail&quot; required&gt;&lt;/br&gt;

&lt;label for=&quot;muname&quot;&gt;User Name:&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;muname&quot; id=&quot;muname&quot; required&gt;&lt;/br&gt;

&lt;label for=&quot;mpass&quot;&gt;Password:&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;mpass&quot; id=&quot;mpass&quot; required&gt;&lt;/br&gt;

&lt;input type=&quot;submit&quot; value=&quot;Membership Register &quot; name=&quot;mregister&quot;&gt;
&lt;/form&gt;
&lt;pre&gt;

functions.php

&lt;/pre&gt;
/* Members */
function members() {
$mname = $_POST['mname'];
$maddress = $_POST['maddress'];
$mphone_num = $_POST['mphone_num'];

$memail = $_POST['memail'];
$muname = $_POST['muname'];
$mpass = $_POST['mpass'];

$userdata = array(
'user_email' =&gt; $memail,
'user_login' =&gt; $muname,
'user_pass' =&gt; $mpass,
);
$user_id = wp_insert_user($userdata);

update_user_meta($user_id, 'm_name', $mname);
update_user_meta($user_id, 'm_address', $maddress);
update_user_meta($user_id, 'm_phone_num', $mphone_num);

$blogusers = get_users( 'role=administrator' );
// Array of WP_User objects.
$user_email=array();
foreach ( $blogusers as $user ) {
$user_email[]=$user-&gt;user_email;
}
$from_email=implode(&quot;,&quot;,$user_email);

$to = array($memail,$from_email);
//$urllink = &quot;http://steve.seomonastery.com/wp-admin&quot;; //get_page_link(54);
$subject = 'Your Admin Password';
$body = 'Your User name is: &lt;b&gt;' . $muname . '&lt;/b&gt; &lt;/br&gt;
Your Password is: &lt;b&gt;' . $mpass . '&lt;/b&gt;&lt;/br&gt;';
wp_mail($to, $subject, $body);
}
add_action( 'wp_ajax_member', 'members' );
add_action( 'wp_ajax_nopriv_member', 'members' );
&lt;pre&gt;

ajax-signup.php

&lt;/pre&gt;
jQuery(document).ready(function($) {

// Perform AJAX send mail and create user on form submit
$('form#ajaxform_member').on('submit', function(e){
$('form#ajaxform_member p.status').show().text(ajax_signup_object.loadingmessage);
$.ajax({
type: 'POST',
dataType: 'json',
url: ajax_signup_object.ajaxurl,
data: {
'action': 'member', //calls wp_ajax_nopriv_ajaxsignup
'mname': $('form#ajaxform_member #mname').val(),
'maddress': $('form#ajaxform_member #maddress').val(),
'mphone_num': $('form#ajaxform_member #mphone_num').val(),
'memail': $('form#ajaxform_member #memail').val(),
'muname': $('form#ajaxform_member #muname').val(),
'mpass': $('form#ajaxform_member #mpass').val(),
},
});
e.preventDefault();
});
});
&lt;pre&gt;

 

%d bloggers like this: