Last active
January 26, 2020 03:59
-
-
Save webhasan/662f279917f678200f9f076485dbe0f2 to your computer and use it in GitHub Desktop.
WordPress Ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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