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.

Have had an autocomplete field working for months but has stopped working when not logged in? Not sure when but within the last few days or week (not updated wordpress recently).

already have; add_action('wp_ajax_filter_schools', 'filter_schools'); add_action('wp_ajax_nopriv_filter_schools', 'filter_schools');

in functions.php and there are no errors anywhere.

response i get when not logged in is;
from safari... *Request URL:http://www.payingforit.org.uk/wp-admin/admin-ajax.php?term=holywe&action=filter_schools&postType=school Request Method:GET Status Code:302 Found*

any help welcome ! Dc.

jquery code

 $( "#userSelectedSchool" ).bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        }).autocomplete({
            source: function( request, response ) {

                $.getJSON( "/wp-admin/admin-ajax.php", {


            term: extractLast( request.term ), action: 'filter_schools', postType: 'school'
            }, response );

            dataToBeSent = {
                term: extractLast( request.term ), action: 'filter_schools', postType: 'school'
            }

            console.log(request.term);

        }, select: function( event, ui ) {

            var terms = split( this.value );
            // remove the current input
            terms.pop();
            // add the selected item
            terms.push( ui.item.id );
            // add placeholder to get the comma-and-space at the end // ui.item.label
            terms.push( "" );
            this.value = ui.item.label;

            $('input[name=userSchool]').val(ui.item.urn)

            return false;

        }, open: function() { $('.ui-menu').width(300) }

});

function in functions.php

add_action('wp_ajax_filter_schools', 'filter_schools');
add_action('wp_ajax_nopriv_filter_schools', 'filter_schools');

function filter_schools(){
    global $wpdb; // this is how you get access to the database

    $str = $_GET['term'];
    $action = $_POST['action'];
    $postType = $_POST['postType'];

    $finalArgs =  array (
        'posts_per_page'=>5,
        'order' => 'ASC',
        'post_type' => 'school'
    );

    $searchSchools = new WP_Query( $finalArgs );
    $mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$str."%' ");

    $args = array(
        'post__in'=> $mypostids,
        'post_type'=>'school',
        'orderby'=>'title',
        'order'=>'asc'
    );

    $res = new WP_Query($args);
    while( $res->have_posts() ) : $res->the_post();

        global $post;

        $EstablishmentNumber = get_post_meta($post->ID,'EstablishmentNumber', true);
        $URN = get_post_meta($post->ID,'URN', true);
        $add = get_post_meta($post->ID,'address', true);

        $schl = array('post_id'=>$post->ID,'id'=>$EstablishmentNumber, 'label'=>$post->post_title.', '.$add['town'].' '.$add['postcode'] , 'value'=>$EstablishmentNumber, 'urn'=>$URN );
        $matchedSchools[] = $schl;

    endwhile;

    echo json_encode($matchedSchools);
    wp_reset_postdata();
    die(); // this is required to return a proper result
}
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Edit: I've kept my original answer below, however, I'm not sure what I was thinking... You should never need to trigger do_action( 'wp_ajax...' ).

While I can't be sure what the issue is, is the code in the question is roughly ok (I think $_POST should be $_GET with .getJSON).


Try putting this at the top...

if(isset($_REQUEST['action']) && $_REQUEST['action']=='filter_schools'):
        do_action( 'wp_ajax_' . $_REQUEST['action'] );
        do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
endif;

I think WordPress does not automatically do ajax actions for users not logged-in. Potentially non-users could things they are not suppose to be able to do.

I would probably change those $_GETs & %_POSTs to $_REQUEST too.

share|improve this answer
    
thanks Stephen - made those updates but its still not working when not logged in. –  daniel Crabbe Jan 12 '12 at 12:51
    
after messing around a bit it seems the do_action has to come after the add_filter. Working now so thanks stephen... –  daniel Crabbe Jan 12 '12 at 15:34
    
I had the same problem with non-admin users. This solution made it, and the order was important in my case too. –  brasofilo Sep 27 '13 at 19:05
add comment

Final working code before the filter_schools() function.

if(isset($_REQUEST['action']) && $_REQUEST['action']=='filter_teachers'):
    add_action('wp_ajax_filter_teachers', 'filter_teachers');
    add_action('wp_ajax_nopriv_filter_teachers', 'filter_teachers');
endif;

if(isset($_REQUEST['action'])):
        do_action( 'wp_ajax_' . $_REQUEST['action'] );
        do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
endif;
share|improve this answer
    
Daniel, the order shouldn't matter: I have a plug-in where the do_actions are at the top, and add_actions come just before the function being called. Also the add_actions don't need to be inside 'if' statements. But if the above works, then it works! –  Stephen Harris Jan 12 '12 at 17:51
add comment

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.