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.
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.
On your local server, perhaps there is redirect from
wp-admin
that redirects to home page unless you’re administrator? If so, you should useDOING_AJAX
const to prevent redirection of ajax calls.For example:
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:
EDIT: Try adding this to your plugin or in the functions.php file of the theme you’re using locally:
A few things you can try:
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.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 mywp-config.php
when developing locally, and then check ifWP_DEBUG
is true when enqueueing my scripts and styles. If so, I replace the version string withtime()
. For example:Clear your browser cache.
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.