WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have on frontend search box, I have implement autocomplete for it, and when I'm logged in, it works great for me.

But when I'm not logged in, I get 302 as result of ajax call, response headers have location in it, so it is trying to redirect, I do not know why. I notice a lot of posts around this issue, but none of the posts/questions didn't help me.

My Code, that works when users are logged in, but doesn't work when users are not logged in, please help.


    type: 'POST',
    url: MyAjax.ajaxurl,
    data: {
        action : 'myajax-submit',
        term : request.term,
        _ajax_nonce : MyAjax.ajax_nonce
    dataType: "json",
    beforeSend: function(jqXHR, settings){
    success: function(data, textStatus, jqXHR){
    error: function(jqXHR, textStatus, errorThrown){
            'value':'Error retriveing data',
    complete: function(jqXHR, textStatus){
    statusCode: {                    


add_action('wp_ajax_myajax-submit', 'myajax_submit');

add_action('wp_ajax_nopriv_myajax-submit', 'myajax_submit');

    function myajax_submit() {
        global $wpdb;

        //check_ajax_referer('myajax_nonce', '_ajax_nonce');

        if (isset($_POST["term"])) {
            $q = strtolower($_POST["term"]);


            $json_response = array();


            header("Cache-Control: no-cache, must-revalidate");
            header("Pragma: no-cache");
            header("Content-type: text/x-json");

            print json_encode($json_response);

share|improve this question
Do you have the final } on myajax_submit? – mor7ifer Mar 2 '12 at 15:20
@m0r7if3r Hi m0r7if3r I have, it was just copy-paste so I accidentally didn't copy last bracket. Tnx. – user1147 Mar 2 '12 at 15:36
up vote 3 down vote accepted

Sounds like you have an issue with another plugin or function that is trying to prevent non logged in or non admin users access to the wp-admin area so it is redirecting from wp-admin/admin-ajax.php and giving you your 302 response.

You need to find the code that is doing this and add a conditional not to redirect if the DOING_AJAX constant is defined.

share|improve this answer
Hi, thank you, that was the problem. Thank you – user1147 Mar 3 '12 at 1:40
did you find the code? Same thing happening to me! – daniel Crabbe Nov 24 '14 at 11:22

A non-admin request... still is an admin request

Inside admin-ajax.php the constant WP_ADMIN is set to TRUE. So if you're including your files wrapped inside for example if ( ! is_admin() ), then your request will abort at the beginning of admin-ajax.php.

share|improve this answer
Thank you for letting me know that – user1147 Mar 3 '12 at 1:40
Had to find that out the hard way myself. :) – kaiser Mar 3 '12 at 14:36

Your Answer


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.