Skip to content

Instantly share code, notes, and snippets.

@webhasan
Last active August 29, 2015 14:11
Show Gist options
  • Save webhasan/c2f55bc599dea7f6af01 to your computer and use it in GitHub Desktop.
Save webhasan/c2f55bc599dea7f6af01 to your computer and use it in GitHub Desktop.
WordPress Ajax implementation
jQuery(document).ready(function() {
var GreetingAll = jQuery("#GreetingAll").val();
jQuery("#PleasePushMe").click(function(){ jQuery.ajax({
type: 'POST',
url: my_ajax_script.ajaxurl,
data: {
action: 'get_my_option',
GreetingAll: GreetingAll,
},
success: function(data, textStatus, XMLHttpRequest){
jQuery("#test-div1").html('');
jQuery("#test-div1").append(data);
},
error: function(MLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}
});
});
});
<?php
/**
* Initiating Script Handaler
*/
function invoce_script(){
wp_enqueue_script('jquery');
wp_enqueue_script( 'action_script', get_template_directory_uri().'/js/ajax-implementation.js', 'jquery');
wp_localize_script( 'action_script', 'my_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('template_redirect', 'invoce_script');
function get_my_option(){
print_r($_POST);
die();
}
add_action("wp_ajax_nopriv_get_my_option", "get_my_option");
add_action("wp_ajax_get_my_option", "get_my_option");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment