I 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?

P.S. 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 WordPress codex but I couldn't see a relevant example in there. I suspect this might have something to do with wp_localise_script() but I'm not sure.


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.

| 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
  • 1
    @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 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.