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.
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, likewp
ortemplate_redirect
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.