How to post form in ajax mode and handle it in wordpress

It does, have you look at Bainternet’s solution to this question and my answer to this question?

To summarise, you need to post the request to the WordPress’ admin-ajax url. If the ajax is for use on the admin-side then this url should already be available as the javascript variable ajaxurl. If this is for use on the front-end then you’ll want to get the admin url:

<?php echo admin_url('admin-ajax.php'); ?>

inside your javascript file (see my solution linked above). WordPress then files the following actions:

//for logged in users only 
add_action('wp_ajax_MY_ACTION', 'my_ajax_function');

//for non-logged in users 
add_action('wp_ajax_nopriv_MY_ACTION', 'my_ajax_function');

Where MY_ACTION is the action sent with the AJAX request. You can hook the function that deals with the request, (in this example my_ajax_function) onto the appropriate hook(s). This function should echo the response.

For more information see the above answers, and the Codex on Ajax in WordPress.

Leave a Comment