I'm trying to use ajax function to my theme. I think i can't just put the javascript code in my functions.php, i have to register it properly and enqueue it.

So i store the javascript file with .js extension and enqueue it from my themes js directory. Now ajax_url is only available globally in admin section not in front end.

To get the url we have to use <?php echo admin_url('admin-ajax.php'); ?> for front end Reference.

How can i use it in the file as it is a js file? Php can not be interpreted.

Is there any other way to define the ajax_url in my js file for front end.

OR should i include it in the header.php with no option?

  • Thanks i got it. can not the same concept used in wp_enqueue_script function itself? there we have depends argument, isn't? There can we specify the admin-ajax – rram Dec 21 '13 at 17:34
  • Should i leave the question or should delete the question? My Issue is solved with the link you have provided @s_ha_dum – rram Dec 21 '13 at 17:35
  • Your choice. I'll mark the question a duplicate though. Upvote the other answer if it helped. – s_ha_dum Dec 21 '13 at 17:46
  • +1. I didn't have enough reputation to give upvote. Thanks – rram Dec 21 '13 at 18:02