[wp-hackers] ajax and $wpdb

scribu scribu at gmail.com
Wed Jul 29 11:23:27 UTC 2009

On Wed, Jul 29, 2009 at 1:41 PM, Adam Taylor <adamjctaylor at gmail.com> wrote:

> Secondly: is this the best way of doing this? I can't find any tutorials or
> documents about writing an ajaxy plugin for wordpress that interacts with
> the database.

Definetly not the best way. I would send you to this page, but it's
outdated, unfortunately: http://codex.wordpress.org/AJAX_in_Plugins

Basically, instead of calling a script directly, you should do this:

For logged-in users:

add_action('wp_ajax_my_action', 'my_ajax_handler');

For the public:

add_action('wp_ajax_nopriv_my_action', 'my_ajax_handler');

The js should look something like this:

jQuery.post(<?php echo admin_url('admin_ajax.php'); ?>, {action:
'my_action', foo: 'bar'});

And the php:

function my_ajax_handler() {

That's about it.


More information about the wp-hackers mailing list