So I'm in the midst of creating a new conversion widget and I'm having some trouble getting the Ajax call to work in my widget. (works perfectly outside of WordPress).

The problem at the moment is that it outputs the whole homepage in the #results div rather than just the results figure:


2 Answers 2


If your AJAX Url is conversion.php it will most likely point to yourdomain.com/conversion.php. Is your file in that location?

Looks like not, and I guess you don't have a 404.php, and in that case your request gets directed to your index.php, that's why you get the contents of your homepage.

If your file is in your theme folder, you would have to change the URL to wp-content/themes/yourtheme/conversion.php

But the much easier and recommended way is to use WP built in AJAX functionality: See this codex page for more informations: http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)

EDIT: Most posts on this topic are for AJAX on the WordPress backend but it's possible to use it on the frontend as well.

  • Thanks mate, appreciate your reply however changing the url produced the same result. Thanks for the link, but if i'm honest that is totally over my head and I have no idea how to get what I have to work with wp_ajax_(action).
    – Danny
    Jan 29, 2012 at 16:40
  • Actually I think i'll give it a shot. If i'm right in thinking i can take the contents of conversion.php (my ajax) and add it to a function which i can can add_action to like this add_action('wp_ajax_conversion', 'my_ajax_function'); and then point url in my jquery to wp_ajax.php ?
    – Danny
    Jan 29, 2012 at 16:47
  • Basically yes. But notice, if you want to use this on the frontend, you have to use wp_ajax_nopriv_conversion instead of just wp_ajax. Jan 29, 2012 at 17:03
  • 1
    To point you request to the right location you should use 'ajaxurl' (which is a global js var). This var is definend everywhere on the backend, but not on the frontend. In that case please see Otto's response at the end of this post: wordpress.org/support/topic/ajaxurl-is-not-defined Additionally you have to define the action in your Jquery Ajax request: should look like: url:ajaxurl, action:conversion, data: ... action = wp_ajax_ actionname Jan 29, 2012 at 17:10

I'm sorry, but some of ungestaltbar's comments are not fully correct.

Couldn't comment above anymore, but it's almost an answer.

When using admin-ajax.php for the front-end ...

One needs to hook BOTH, wp_ajax_(action) & wp_ajax_nopriv_(action) on front- AND back-end, else it won't work.

The reason why you get the whole page returned might be that the XHR call is being intercepted by rewrite rules - WP is a single point of access application.

The syntax is about like that:

add_action('wp_ajax_nopriv_conversion', array(&$this, 'do_conversion'));
add_action('wp_ajax_conversion', array(&$this, 'do_conversion'));

This might sound not too logical, but I'm pretty sure with that (recently used in a project).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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