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 plete there is nothing on the response tab of console screen.How e this happen?
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 plete there is nothing on the response tab of console screen.How e this happen?
Share Improve this question asked Jan 25, 2013 at 12:01 saidozcansaidozcan 2,2159 gold badges29 silver badges40 bronze badges 5- Do you have a question? Are you saying the code doesn't do what you expect it to do? – MrCode Commented Jan 25, 2013 at 12:03
- sorry i have edited my question – saidozcan Commented Jan 25, 2013 at 12:05
- Check your PHP error log, are there any errors? – MrCode Commented Jan 25, 2013 at 12:06
- What does the console say the response code is? 200, 500, 404? – MrCode Commented Jan 25, 2013 at 12:08
- oi46.tinypic./9lh6s9.jpg it's screen shot of response.I guess it's succesfully returns 200. – saidozcan Commented Jan 25, 2013 at 12:11
2 Answers
Reset to default 4WordPress 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:
//for logged in users
add_action('wp_ajax_my_action', 'my_action_callback');
//for not logged in users
add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
function my_action_callback() {
//your function here
exit; //always call exit at the end of a WordPress ajax function
}
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):
$.ajax({
url : '/wp-admin/admin-ajax.php',
type : 'get', //or 'post'
data : {
action: 'my_action_callback'
}
})
.fail(function(r,status,jqXHR) {
console.log('failed');
});
.done(function(r,status,jqXHR) {
console.log('success');
});
Note the my_action_callback parts.
Make sure you have enqueued your custom script as follows. If your script is included before the Wordpress jQuery scripts are custom injected your code might not work properly.
function my_scripts_method() {
wp_enqueue_script(
'custom-script',
get_template_directory_uri() . '/js/main.js',
array('jquery')
);
}
add_action('wp_enqueue_scripts', 'my_scripts_method');