i’ve done an ajax request in my wordpress plugin.My request is based on steps below:
1) i am including js file and starting a request document on ready
2)At the php side i want to take this request and print it to test.
for all these stuff i wrote these codes:
main.js:
$(function(){
$.post('/wp-content/plugins/dsn/functions.php',{token:"1tibu4"},function(data) {
console.log(data);
},"json");
});
functions.php:
<?php
add_action('loop_end', 'init');
function init(){
global $_POST;
$post= $_POST;
echo json_encode($post);Exit;
}
My question is when request complete there is nothing on the response tab of console screen.How come this happen?
WordPress has a unique way of handling AJAX. If you make a request to the php file directly, then you are not loading the rest of the WordPress framework and a lot of WordPress specific functionality will not be available to you.
In the PHP side, what you have to do is use code that looks like this:
In the JS side, you need to send your request to a file called “wp-admin/admin-ajax.php”
So your request would look like this (using jQuery):
Note the my_action_callback parts.