Wp ajax Not working on local wamp unless logged in as admin

I have written a WordPress plugin which uses Ajax to update the client and appears to work fine on the production server. The same plugin on my local wampserver only returns correct Ajax responses if logged in with full administrator priviledges – all other users (including Public) receive an html response which is just the homepage!

This is obviously a setup problem with my wampserver, but I am confused with the fact that I can get the plugin to work (only) if I am logged in as administrator.

Read More

Any pointers to help identify the problem would be greatly appreciated.

Thanks to several of the initial responses, I have done some more investigation and believe the there is probably a redirect occurring BEFORE my registered Ajax hook calls its php function unless one is logged in as administrator. I believe that I am following all the correct Ajax setups (hooks registered both for normal and no-priv, cache set false, json encoded returns and terminating in wp_die(), etc.). I have also found a post where someone had a similar problem 10 months ago (‘whitenoisedb’, asked Apr 24’15 at 7:55) it and was ‘solved …. handling the AJAX inside front-page.php because it was impossible by using admin- Ajax.php’ and he was still wondering why it could not be done the ‘right’ way!

To re-iterate, the problem only occurs on my local wampserver development server and continues to mystify me.

Related posts

3 comments

  1. On your local server, perhaps there is redirect from wp-admin that redirects to home page unless you’re administrator? If so, you should use DOING_AJAX const to prevent redirection of ajax calls.
    For example:

    function admin_redirect()
    {
        if (is_admin() && !current_user_can('administrator') && !defined('DOING_AJAX'))
        {
            wp_safe_redirect(home_url());
        }
    }
    
    add_action('admin_init', 'admin_redirect');
    
  2. I ran into this a while back. Check out this section of the codex where it talks about “Ajax on the Viewer-Facing Side”. There are two actions. One for people logged in, and one for everybody else. Specifically:

    add_action( 'wp_ajax_my_action', 'my_action_callback' );
    add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
    
  3. EDIT: Try adding this to your plugin or in the functions.php file of the theme you’re using locally:

    add_action( 'send_headers', 'add_custom_headers' );
    
    function add_custom_headers() {
    
        if ( true !== WP_DEBUG ) {
            return;
        }
    
        header( 'Expires: Tue, 01 Jan 2000 00:00:00 GMT' );
        header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
        header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' );
        header( 'Cache-Control: post-check=0, pre-check=0', false );
        header( 'Pragma: no-cache' );
    }
    

    A few things you can try:

    1. Make sure you’re disabling the cache in your AJAX calls: $.ajax({ url: '...', cache: false }). You can also use $.ajaxSetup({ cache: false }) but I would recommend against it since it applies the setting globally and could interfere with other plugins and/or themes.

    2. Use a random string for the script version to prevent it from being cached. What I usually do is set WP_DEBUG to true in my wp-config.php when developing locally, and then check if WP_DEBUG is true when enqueueing my scripts and styles. If so, I replace the version string with time(). For example:

      $version = ( true === WP_DEBUG ) ? time() : '1.0.0';
      wp_enqueue_script( 'script', '...', array(), $version, true );
      
    3. Clear your browser cache.

    4. If you have a caching plugin installed locally, make sure you clear the cache there as well.

    If none of that works, try restarting the WAMP process or just restart your computer.

Comments are closed.