[wp-hackers] Plugins: Best practice for conditional loading of actions and file includes
Austin Matzko
austin at ilfilosofo.com
Tue Feb 9 21:27:16 UTC 2010
On Tue, Feb 9, 2010 at 3:14 PM, Otto <otto at ottodestruct.com> wrote:
> http://codex.wordpress.org/AJAX_in_Plugins
>
> Basically, you want to make your javascript fire the request at
> "admin_url('admin-ajax.php')". The request will need to include action
> = 'some_special_action' in the POST.
>
> Then you hook a function to wp_ajax_nopriv_some_special_action. The
> function will get called for your action and it can get the stuff out
> of the POST and act on it. Then it makes its own output and can call
> exit or die to end the request quickly.
Since the question was about Ajax on the "front-end," it's worth
pointing out that if someone has forced SSL for the admin, the link
printed by admin_url() might not be useable for XHR because it
violates the same origin requirement. (In other words, the front end
would be using a different protocol: "http" instead of "https").
For front-end Ajax it's probably more reliable to post to the home URL
or wp-load.php and have a callback attached to "init" listening for
the requisite POST arguments.
More information about the wp-hackers
mailing list