WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What processing is done by wordpress when a page is accessed that loads all contextual information into $post and other variables? and is it possible to trigger this process manually (or simulate it) so function calls have this context?

The reason I'm asking is because I have written a small plugin that allows users to call wordpress functions via Ajax.

The plugin works great with context-less functions. It has a big limitation however: calling a function via ajax loses the context information (e.g. $post) that the called function would otherwise have, had it been placed from within a template.

However, the ajax call does send across the HTTP Referrer information. I can easily extract the referrer info, so I know e.g. on which page the user is on. The problem is I'm not sure how to use it further. Any tips/ideas would be greatly appreciated.

share|improve this question
up vote 0 down vote accepted
  • you're requesting index.php which I guess it's the homepage; if I call for example is_category() trough your ajax from the category page I'll get false because the function is being ran on the homepage. Use $_SERVER["REQUEST_URI"] instead (don't forget to sanitize it)

  • you're hooking your handler on the init action, which I think runs before the post data is set up. Use a different one, like wp or template_redirect

share|improve this answer
Great stuff. Still need to put it all together, but definitely looks like this is the direction I need to take. I'm thinking of using javascript instead of $_SERVER["REQUEST_URI"] though to pick the right target url. Then the hook change really makes the difference! I have to admit I'm still not particularly clear about processing order / hook points etc in wordpress. Thanks One Trick Pony! – Yoav Aner Jun 30 '11 at 8:16

To use WordPress functions from ajax call you need to hook your own function to wp-admin/admin-ajax.php

to get a better understanding take a look at This.

share|improve this answer
That's almost the same as what he's currently doing. He wants those functions to be aware of the current page context, admin-ajax.php doesn't accomplish that – onetrickpony Jun 29 '11 at 23:25
you can always send the current page context with your ajax post – Bainternet Jun 30 '11 at 8:31
Can you elaborate please? – Yoav Aner Jun 30 '11 at 9:37
sure, on what? on sending the context, say you are on a page you can add to your ajax call something like context: 'page' then in your ajax function you can see that the context is a page – Bainternet Jun 30 '11 at 9:55
Oh, but that's not what the plugin does. It attempts to generalise ajax calls to ANY function, and the function grabs whichever context it needs. I can't force all functions to know about this context variable... The ajaxize plugin simply wraps around other functions... Thanks though. Looks like I'll stick to One Trick Pony's solution – Yoav Aner Jun 30 '11 at 10:05

Your Answer


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.