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.

So i'm using this configuration for an AJAX call in the administrative area (like this):

add_action( 'admin_head', 'ajaxPerNgg' );

function ajaxPerNgg(){ ?>
<script type="text/javascript" >
jQuery(document).ready(function() {
    jQuery("#message").remove();
    jQuery("#errore a#attivo").click(
        function(){
            var data = {
                action: 'my_action'
            };
            jQuery.post(ajaxurl, data, function(response) {
                if(response == "attivato")
                    jQuery("#errore a#inattivo").attr("id","completato").html("Attivato");
                else
                    jQuery("#errore a#inattivo").attr("id","attivo").html("Attivalo ora");
            });
        }
    );
});
</script>
<?php }

add_action('wp_ajax_my_action', 'my_action_callback');

function my_action_callback() {
    echo 'attivato';
    die();
}

It's just a test code, but it always return 0 instead of the string "attivato". It also find the ajax url correctly, without a 404 error.
What is the problem?

share|improve this question
    
Did you ever get this problem figured out? If so, can you please comment (or accept) the answer that solved your problem? If none of these solved your problem and it is solved, could you please add an answer below? Thank you! –  jmort253 Jul 5 '12 at 23:51
    
@jmort253 hi, sorry to answer you right now, but at the moment, i still have this problem and i found no solution around the web :'( i'm working on it when i have some spare time; if i find the solution, i'll write here ;) –  Ivano IsayBlog Mercuri Jul 6 '12 at 6:56
    
Ok, same here. If I figure it out, I'll post. :) Keep in touch. –  jmort253 Jul 6 '12 at 7:27
    
I solved my problem, the suffix of the wp_ajax_my_action, the "my_action" part, must match the action used in the jQuery post. I see that yours matches, so that's not the problem in your case. I also have it working from the admin page, which is what you're trying to do. Are you logged in when trying to make this ajax call? Or are you logged out? –  jmort253 Jul 6 '12 at 16:13
    
Right. Didn't notice that! ;) –  Rutwick Gangurde Jul 7 '12 at 4:44

3 Answers 3

Add this as well: add_action('wp_ajax_nopriv_my_action', 'my_action_callback');. Notice the nopriv.

It will solve your problem!

share|improve this answer
1  
I fixed this by making sure the suffix of "wp_ajax_SUFFIX" matches the action, so that the action in the jQuery post was also "SUFFIX". However, I didn't need to use nopriv... Not sure if this is the answer to the op's problem, since this is an action that's occurring on the admin page, which pretty much means you're logged in, which suggests you don't need to register a nopriv handler... –  jmort253 Jul 6 '12 at 16:15

I had the same problem. Solution:

add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
add_action('wp_ajax_my_action', 'my_action_callback');

If you dont add both (with and without nopriv) it will work in either signed in mode or signed out mode only. nopriv is for signed out, the other is for signed in.

share|improve this answer

Not sure if this is your problem, but I was getting a response of just "0" and it took me the longest time to figure it out. Aside from making sure you put exit or die at the end of your php function, I found out that you have to put your add_action('wp_ajax_... bit at the top level of your plugin. I had it nested inside my admin_menu hook, which didn't work. I'm not sure if your code above is part of an include inside a function from your top level plugin php file (like mine was).

share|improve this answer
    
The same problem here.. Not sure how to fix this kind of issue, since we sometimes need to put the ajax response inside or at the end part of codes. –  Ari Susanto Dec 16 '13 at 7:52

protected by Community Dec 2 '12 at 9:32

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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