admin ajax parameters
-
Hello,
I want to display a list of sub-pages in each main page, and then include the children’s content through AJAX if Javascript is enabled instead of opening a new page.
I used to rely on a separate template PHP file to handle the requests, but now I feel I have to follow the recommendations
http://codex.ww.wp.xz.cn/AJAX_in_Plugins
http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/So my list output looks like this:
<a id="page_id_11" class="ajax_trigger_link" href="http://myblog.example.com/parentpageslug/childrenpage1slug">children page 1</a> <a id="page_id_12" class="ajax_trigger_link" href="http://myblog.example.com/parentpageslug/childrenpage2slug">children page 2</a>The Javascript part enables Ajax and prevents the links from performing their default action.
Assuming that I’m not using jQuery but MooTools, and that the action hooks to admin-ajax are correctly set, the crucial parameters of the AJAX request should be:
url : admin_url( ‘admin-ajax.php’ )
and
data = {
action: ‘my_special_action’, //the POST parameter that wordpress requires to look for this action.
whatever: 1234 //additional post values
};The ‘whatever’ parameter is intended to be the target of the AJAX request: nothing is simpler than the page/post ID, I think, and I could get it by stripping the link element ID or somehow via the wp_localize_script function (a bit more complicated, but wait a moment, there could be a reason to do that).
Now in my theme’s functions.php I have to set the ‘my_special_action’ function and make it retrieve the content according to the other parameter added along with ‘action’, namely ‘whatever’ in this case. I should call get_post() for posts, get_page() for pages, etc.
At this point my question is: how could I make it work regardless of whether it is a page or a post, a category, a tag or really ‘whatever’?
Do I absolutely need another parameter for ‘type definition’ (again, by stripping the link HTML attributes in JS or by declaring with wp_localize_script)?
Or is there a way to simply pass the link href (maybe after urlencode) as ‘whatever’ target parameter to admin-ajax.php instead of the ID?
The topic ‘admin ajax parameters’ is closed to new replies.