WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

am trying out a bit of ajax ( via jquery ) in wordpresss. ... i have everything working correctly but for some reason i have to have the full url to the php handler file even though it's located in the same directory as the script

here's the code

$.post('http://full/url/to/file.php', $("#form").serialize(), function(data){ do stuff ... });

why cant i just put "file.php" into that - why does it need the full url .... or more importantly what can i put in there that would work on any site without the end user having to put in the full url every time

ps i have read the problem with ajax and the path to the php page but i just got a bit more mixed up - i have also read the ajax in plugins page on the wp codex but i couldnt see a relevant example in there .... i'm suspect this mught have something to do with wp_localise_script but im not sure


share|improve this question
up vote 1 down vote accepted

it needs a full URL because even though it may be the right location relatively on the back end, it's not on the same on the front end, at the URL where your page is ultimately served.

you're on the right path with wp_localize_script. you want to enqueue your ajax script, then pass the admin ajax url to wp_localize_script:

function my_init_method(){
    wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/my_ajax_script.js', array( 'jquery' ) );
    wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
add_action('init', 'my_init_method');

now within your ajax script you can refer to MyAjax.ajaxurl for the URL.

Check this post for a great writeup on properly using ajax in plugins and themes, this is what the WP Codex links to as an example.

share|improve this answer
thanks .... that makes complete sense .... however i spent ages trying to get localise script working but couldn't .... i find it very confusing every time i see a reference to wp_localize_script it is setting up the referer url to point to admin-ajax.php ( which i understand must be used even when doing viewer facing ajax) but what i dont understand is A) how do i get my own php script into the picture and B) what use admin-ajax.php is to me ( i mean the script works fine without it but it seems like in wordpress you must send you're info through that or something ) ... any help appreciated – byronyasgur Jun 21 '11 at 0:36
@byronyasgur - the purpose of admin-ajax.php is to provide a central means of managing ajax requests. rather than calling your php script directly, you hook all of your ajax functions into wp_ajax and wp_ajax_nopriv actions. these will then be triggered by your ajax calls to admin-ajax.php when you pass the associated action. visit the link I posted above and click 2. Use admin-ajax.php to handle AJAX requests for the code sample. – Milo Jun 21 '11 at 2:00
right - it's beginning to make sense now ... i think i was trying to impliment that page one section at a time but 1 and 2 are kinda dependant on one another it seems ....so admin-ajax calls my script when i hook it onto wp_ajax_ and wp_ajax_nopriv ??? i will try it out tomorrow .... thanks for your help .... by the way for anyone else reading this i came up with a lot of errors which stumped me for a while until i read that you shouldnt use the $ shortcut for jquery in wordpress but use jQuery instead – byronyasgur Jun 21 '11 at 2:24
finally got it figured - thanks so much for the help – byronyasgur Jun 23 '11 at 20:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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