I am developing an admin-only plugin, which uses some ajax calls in its interface. The website itself also relies on ajax calls on its front-end, too.
I’d like to be able to identify when the WP is loaded due to “simple backend call”, “AJAX backend call” or “AJAX frontend call”. The use case, in its simplest form, could be to load my plugin only on admin pages or ajax calls from them. Besides, it helps not to break the front-end while developing on a live site 🙂
So far, I found the following:
is_admin()
returns true on all AJAX calls (which is strange, imho)- as all my AJAX calls are done via jQuery, I can determine an AJAX call by HTTP variable
HTTP_X_REQUESTED_WITH
(see here).
But I still cannot figure out how to distinguish between admin-side AJAX and front-side AJAX. Any ideas?
I understand the problem you are having, I was thinking of the same thing.
It is not really a problem tho, it’s more a matter of good programming design and have a good separation of front-end and back-end.
Now the way I have managed to solve this is doing the following:
I hope you are calling your AJAX action with
wp_localize_script
. We are going to extend this is a little by implementing it this way:This is ONLY added in the frontpage, I am sure you know how this works. Now as you know this will add a little piece of javascript. Note here the variable
my_frontend_call
, this is the important one we are going to use.More instructions on how to set up a good ajax call here: link
Now we have this all set up, now we can easily implement the logic in the backend as this:
Now you can mix this up a little with different variables from different locations.
I don’t know why this isn’t a known recommendation. In my opinion it’s ugly when stuff get’s loaded when it is not even needed.
NOTE I still am new to WordPress, so maybe this is not a good method, I hope some of the professionals here can comment on this.
This is not strange, because Ajax calls follow much more of admin load logic, than front-end. Essentially Ajax load is specialized version of admin load.
Not sure what you mean here. Script queueing does not depend on Ajax. If you don’t want script on front end – don’t load it there.
Modern WordPress does not rely on referrers and such for detection where call came from, instead nonces should be used for source and intent verification.
Since it is your own code than simplest way to distinguish request is to have request carry explicit argument with information is it from front or back.
My idea is very simple.
Just define two different names for the nonce field. (So, other than the developer, no one will know which name for which purpose.) Example:
The nonce data should be with each ajax call to secure your script according to http://codex.wordpress.org/AJAX_in_Plugins .