Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Okay, I'm starting out with some AJAX stuff in a wordpress theme

1) I'm building a child theme OFF the thematic framework 2) My child theme has a header.php, index.php, functions.php and style.css (at this stage)

In my header.php I have the following (btw: the code is adapted from http://codex.wordpress.org/AJAX_in_Plugins):

<?php
    if( !is_admin() ) {
 add_action('wp_ajax_my_special_action', 'my_action_callback');
 add_action('wp_ajax_nopriv_my_special_action', 'my_action_callback');
 $_ajax = admin_url('admin-ajax.php');    
}
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {

 var data = {
  action: 'my_special_action',
  whatever: 1234
 };

 jQuery.post('<?php echo($_ajax); ?>', data, function(response) {
  jQuery('#output').html('Got this from the server: ' + response);
 });

});
</script>
</head>

Right so that's all cool - and it updates the OUTPUT div on the page with "Got this from the server: 0"

I need a PHP function called "my_action_callback" - so in my theme's functions.php I have the following:

function my_action_callback() {

 $whatever = $_POST['whatever'];

 $whatever += 10;

    echo 'whatever now equals: ' . $whatever;

 die();
}

This is the ONLY function in my functions.php

To make sure the PHP function is working I stick my_action_callback() into my index.php - and it outputs "whatever now equals: 10" as expected.

HOWEVER - the AJAX response is always 'Got this from the server: 0' Ajax never seems to get the response from the PHP function.

I tried adding .ajaxError() to see if there were any errors - nope.

I tried adding the PHP functions to another plugin of mine - nope.

What am I missing that jQuery isn't doing the ajax bit for me?

Thanks in advance

share|improve this question

1 Answer 1

up vote 6 down vote accepted

Put those add_action functions in your functions.php file too. If they're in header.php, WordPress never registers them since header isn't loaded in AJAX. Also, you don't need that is_admin() check. The theme's header will never load in admin. So, your functions file should look like this:

add_action('wp_ajax_my_special_action', 'my_action_callback');
add_action('wp_ajax_nopriv_my_special_action', 'my_action_callback');
function my_action_callback() {

 $whatever = $_POST['whatever'];

 $whatever += 10;

    echo 'whatever now equals: ' . $whatever;

 die();
}

And the beginning of that part of your theme's header file should look like this:

<?php
$_ajax = admin_url('admin-ajax.php');
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {

Other than that, your code looks like it's good to go!

share|improve this answer
1  
Though I'd replace "my_special_action" with something that makes sense in the context of your theme ... –  EAMann Oct 1 '10 at 14:21
    
THANK YOU!!! That's exactly what I needed. I thought it'd be a matter of putting the right thing in the right place, just wasn't sure what! Thank you so much –  keranm Oct 2 '10 at 1:48
    
@EAMann - yes I'll be naming things correctly :) he he .. just trying out my first AJAX functions for wordpress before writing proper functions. Good comment though :) –  keranm Oct 2 '10 at 1:49
    
why would your run admin-ajax.php? Sounds scary. You can just query the current page url... –  onetrickpony Jan 3 '11 at 18:26
    
You run admin-ajax.php because that's how it's done. It provides a forwards and backwards compatible approach and neatly segregates the rendering engine from the ajax functionality being executed. Don't go around the mountain if there's a tunnel right in front of you. –  John P Bloch Jan 3 '11 at 20:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.