I have a custom search I have created that is handled by an ajax call. It functions correctly for the admin but not for non-admin, but logged in, users.

Admin will get the expected results returned. A logged in user that is NOT admin will get an ajax error and never even reach the memberSearchAjax function that is added with

add_action( 'wp_ajax_memberSearchAjax', 'memberSearchAjax' );

It's like when the ajax request attempts to hit admin-ajax.php it fails in some manner, but only if the logged in user is NOT admin.

My question... why might this Ajax request only work for Admin and not for other logged in users?

Some code...

I enqueue and localize my custom script:

function memberSearch_scripts() {
    wp_enqueue_script( 'memberSearch', get_stylesheet_directory_uri() . '/js/memberSearch.js', array( 'jquery' ) );
    wp_localize_script( 'memberSearch', 'memberSearchObject', array(
            'ajaxurl' => admin_url( 'admin-ajax.php' ) )

//add_action( 'wp_enqueue_scripts', 'memberSearch_scripts' );
add_action( 'init', 'memberSearch_scripts' );

Then a fairly complex function named memberSearchAjax handles the search. I use add_action on this like so:

add_action( 'wp_ajax_memberSearchAjax', 'memberSearchAjax' );
add_action( 'wp_ajax_nopriv_memberSearchAjax', 'memberSearchAjax' ); <-- added this to see if it would help... doesn't

My javascript ajax looks like this:

$('#searchMembersForm').on('submit', function (e) {
            var spinner = '<div class="ajax-spinner"><img src="/wp-content/themes/foobar/images/ajax-loader.gif" /></div>';
                type: 'POST',
                dataType: 'json',
                url: memberSearchObject.ajaxurl,
                data: {
                    'action': 'memberSearchAjax',
                    'firstName': $('#firstName').val(),
                    'security': $('#security').val(),
                    'datasource': 1
                error:function(jqXHR, textStatus, errorThrown){
                    $('#memberSearchResults').html('<p>There was an error with your request. It is: <br>' + errorThrown.message + '</p>');
                success: process

The success function, named "process" is quite complex so I'm not including it but non-admin users never reach it anyway.

1 Answer 1


OK... this turns out to be due to a chunk of code I thought was unrelated. In an effort to keep users out of wp-admin I had added:

function foobar_redirect_admin() {
    if( !current_user_can( 'edit_posts' ) ) {
        wp_redirect( site_url() );

add_action( 'admin_init', 'foobar_redirect_admin' );

And that would redirect non-admin users away from admin-ajax.php as well as the wp-admin backend which caused the ajax request to fail.

  • +1 I also made a mistake with aborting on is_admin() once, scratching my head for a day why this wouldn't work - turned out that AJAX always is admin.
    – kaiser
    Oct 31, 2013 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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