currently I can only do it in a normal non-wordpress way.

So, does wordpress provide a solution for this circumstance?


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.