Skip to content

Instantly share code, notes, and snippets.

@webhasan
Last active January 26, 2020 03:59
Show Gist options
  • Save webhasan/662f279917f678200f9f076485dbe0f2 to your computer and use it in GitHub Desktop.
Save webhasan/662f279917f678200f9f076485dbe0f2 to your computer and use it in GitHub Desktop.
WordPress Ajax
<?php
function statar_scripts() {
wp_enqueue_script( 'main-js', get_template_directory_uri() . '/js/main.js', array('jquery'), '20151215', true );
wp_localize_script( 'main-js', 'ajax_url', admin_url( 'admin-ajax.php' ));
}
add_action( 'wp_enqueue_scripts', 'statar_scripts' );
/**
* Ajax Calling
*/
add_action( 'wp_ajax_my_action', 'my_action_cb' ); // for logged user
add_action( 'wp_ajax_nopriv_my_action', 'my_action_cb' ); // for not logged user
function my_action_cb() {
print_r($_POST);
die;
}
;(function($) {
$.ajax({
url: ajax_url,
method: 'POST',
data: {
action: 'my_action',
value: 'some testing value'
},
success: function(data) {
console.log(data);
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment