Category Archives: Ajax

Add Download Button and Counter Using WordPress Ajax

Add below code into functions.php

/* Enque script for ajax */
add_action('wp_enqueue_scripts', 'download_counter_enqueue');
function download_counter_enqueue() {
	wp_register_script('ajax-download-script', get_template_directory_uri() . '/js/dowload-counter.js', array('jquery'));
	wp_enqueue_script('ajax-download-script');
	wp_localize_script('ajax-download-script', 'ajax_download_object', array('ajaxurl' => admin_url('admin-ajax.php')));
}  

/* Insert download counter using ajax */
function download_counter() {
    //update_post_meta(45, 'themes_downloads', 45);
    $wp_id = $_POST['wp_id'];
    $wp_downloaded = $_POST['wp_downloaded'];
   
    $wp_downloads_count = $wp_downloaded + 1;
    $wp_downloads = update_post_meta($wp_id, 'wp_downloads', $wp_downloads_count );
}
add_action( 'wp_ajax_download', 'download_counter' );
add_action( 'wp_ajax_nopriv_download', 'download_counter' );

Create new javascript file (dowload-counter.js)

/* WP themes Download counter */	
jQuery(document).ready(function($) {
	 $("a").click(function(){
     $("#ajaxform").submit();
     $.ajax({
            type: 'POST',
            dataType: 'json',
            url: ajax_download_object.ajaxurl,
            data: {
                    'action': 'download', //calls wp_ajax_nopriv_ajaxdownload
                    'wp_id': $('form#ajaxform #wp_id').val(),
                    'wp_downloaded': $('form#ajaxform #wp_downloaded').val(),
                  },
            success: function(){ }
        });
  });
});

Add below form and button code in sigle file (single.php)

$wp_downloaded = get_post_meta($post->ID, 'wp_downloads', true); ?>
<form id="ajaxform" action="ajaxform" method="post">
    <input type="hidden" name="wp_id" id="wp_id" value="<?php echo $post->ID; ?>" />
    <input type="hidden" name="wp_downloaded" id="wp_downloaded" value="<?php echo $wp_downloaded; ?>" />
    <input type="submit" name="submit" style="display:none;"/>
</form>
<a href="#">Download</a> <?php echo $wp_downloaded;

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' );

Submit two form by one button and redirect with jQuery Ajax

&lt;/pre&gt;
&lt;form id=&quot;ajaxform_member&quot; action=&quot;&quot; method=&quot;post&quot;&gt;
&lt;table class=&quot;signup-tbl&quot;&gt;
&lt;tr&gt;
&lt;td&gt;&lt;label for=&quot;mname&quot;&gt;Name:&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; required=&quot;&quot; id=&quot;mname&quot; name=&quot;mname&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;label for=&quot;maddress&quot;&gt;Address:&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;maddress&quot; id=&quot;maddress&quot; required&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;label for=&quot;mphone_num&quot;&gt;Phone Number:&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;mphone_num&quot; id=&quot;mphone_num&quot; required&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;label for=&quot;memail&quot;&gt;Email:&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;email&quot; name=&quot;memail&quot; id=&quot;memail&quot; required&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;label for=&quot;muname&quot;&gt;User Name:&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;muname&quot; id=&quot;muname&quot; required&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;label for=&quot;mpass&quot;&gt;Password:&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;password&quot; name=&quot;mpass&quot; id=&quot;mpass&quot; required&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;input  class=&quot;signup-tbl-btn&quot; id=&quot;form-ajax-submit&quot; type=&quot;submit&quot; value=&quot;Membership Register&quot;  name=&quot;mregister&quot;&gt;
&lt;p class=&quot;status&quot;&gt;&lt;/p&gt;
&lt;/form&gt;

&lt;form action=&quot;https://www.sandbox.paypal.com/cgi-bin/webscr&quot; method=&quot;post&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;business&quot; value=&quot;raviradadiya.ommune-facilitator@gmail.com&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;cmd&quot; value=&quot;_xclick-subscriptions&quot; /&gt;
&lt;!-- Instant Payment Notification &amp; Return Page Details --&gt;
&lt;input type=&quot;hidden&quot; name=&quot;notify_url&quot; value=&quot;http://localhost/JobInterviewGuide/?s2member_paypal_notify=1&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;cancel_return&quot; value=&quot;http://localhost/JobInterviewGuide/&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;return&quot; value=&quot;http://localhost/JobInterviewGuide/?s2member_paypal_return=1&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;rm&quot; value=&quot;2&quot; /&gt;
&lt;!-- Configures Basic Checkout Fields --&gt;
&lt;input type=&quot;hidden&quot; name=&quot;lc&quot; value=&quot;&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;no_shipping&quot; value=&quot;1&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;no_note&quot; value=&quot;1&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;custom&quot; value=&quot;localhost&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;currency_code&quot; value=&quot;USD&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;page_style&quot; value=&quot;paypal&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;charset&quot; value=&quot;utf-8&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;item_name&quot; value=&quot;Bronze Member / description and pricing details here.&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;item_number&quot; value=&quot;1&quot; /&gt;
&lt;!-- Configures s2Member's Unique Invoice ID/Code  --&gt;
&lt;input type=&quot;hidden&quot; name=&quot;invoice&quot; value=&quot;&lt;?php echo S2MEMBER_VALUE_FOR_PP_INV(); ?&gt;&quot; /&gt;
&lt;!-- Identifies/Updates An Existing User/Member (when/if applicable)  --&gt;
&lt;input type=&quot;hidden&quot; name=&quot;on0&quot; value=&quot;&lt;?php echo S2MEMBER_CURRENT_USER_VALUE_FOR_PP_ON0; ?&gt;&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;os0&quot; value=&quot;&lt;?php echo S2MEMBER_CURRENT_USER_VALUE_FOR_PP_OS0; ?&gt;&quot; /&gt;
&lt;!-- Identifies The Customer's IP Address For Tracking --&gt;
&lt;input type=&quot;hidden&quot; name=&quot;on1&quot; value=&quot;&lt;?php echo S2MEMBER_CURRENT_USER_VALUE_FOR_PP_ON1; ?&gt;&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;os1&quot; value=&quot;&lt;?php echo S2MEMBER_CURRENT_USER_VALUE_FOR_PP_OS1; ?&gt;&quot; /&gt;
&lt;!-- Controls Modify Behavior At PayPal Checkout --&gt;
&lt;input type=&quot;hidden&quot; name=&quot;modify&quot; value=&quot;0&quot; /&gt;
&lt;!-- Customizes Prices, Payments &amp; Billing Cycle --&gt;
&lt;!--&lt;input type=&quot;hidden&quot; name=&quot;amount&quot; value=&quot;0.01&quot; /&gt;--&gt;
&lt;input type=&quot;hidden&quot; name=&quot;src&quot; value=&quot;1&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;srt&quot; value=&quot;&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;sra&quot; value=&quot;1&quot; /&gt;
&lt;!--&lt;input type=&quot;hidden&quot; name=&quot;a1&quot; value=&quot;0&quot; /&gt;--&gt;
&lt;!--&lt;input type=&quot;hidden&quot; name=&quot;p1&quot; value=&quot;0&quot; /&gt;--&gt;
&lt;!--&lt;input type=&quot;hidden&quot; name=&quot;t1&quot; value=&quot;D&quot; /&gt;--&gt;
&lt;input type=&quot;hidden&quot; name=&quot;a3&quot; value=&quot;0.01&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;p3&quot; value=&quot;1&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;t3&quot; value=&quot;M&quot; /&gt;
&lt;!-- Displays The PayPal Image Button --&gt;
&lt;input class=&quot;paypal&quot;  type=&quot;submit&quot; src=&quot;https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif&quot; style=&quot;width:auto; height:auto; border:0;&quot; alt=&quot;PayPal&quot; /&gt;
&lt;/form&gt;
&lt;pre&gt;

ajax.signup.js

&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(),
},
success: function (response) {
//window.location.href='http://localhost/JobInterviewGuide/paypal.php';
}
});
e.preventDefault();
});
$(&quot;.paypal&quot;).click(function(){
alert(&quot;The paragraph was clicked.&quot;);
$(&quot;#form-ajax-submit&quot;).trigger(&quot;click&quot;);
//$(&quot;paypal&quot;).trigger(&quot;click&quot;);
});
});
&lt;pre&gt;

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;

 

Live css and js files

jQuery min.js

http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js

%d bloggers like this: