WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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

I have HTML5 Template with 5 pages. I am converting it to a WordPress Theme.

I have a contact page. This page includes 'forms.js' script to send contact form data to 'MailHandler.php' file.

Inside the 'forms.js' script there is a variable I want to change,


How do I change this to something like the following?

mailHandlerURL:<?php echo get_template_directory_uri(); ?>'/bat/MailHandler.php'

The PHP is not working inside this script. Please help.

share|improve this question
you don't have to provide absolute url, just use the relative path to MailHandler.php – Hameedullah Khan Aug 27 '12 at 9:53
Is that AJAX form and you need MailHandler.php for that? – Mamaduka Aug 27 '12 at 10:42
@HameedullahKhan it does not work that way. I tried... – Upeksha Aug 27 '12 at 17:03
@Mamaduka No. It is just a POSTing to the MailHandler.php – Upeksha Aug 27 '12 at 17:06
up vote 1 down vote accepted

If you want to send the url of the website, or other server side calculated values, to JavaScript you can use wp_localize_script (Codex Page). This function creates an object, with the values you've passed.

As the documentation states, you've got to include your script with wp_enqueue_script, else it doesn't call localize_script. Also make sure to call wp_head() before your closing head tags, to make it work.

share|improve this answer
Thanks. this is the exact answer I was looking after. I just couldn't find it. Thanks martinCzerwi! – Upeksha Aug 27 '12 at 17:09

If the code itself is small, then I think you should call it inside theme file, as shown here in the below example.

Example -

mailHandlerURL:<?php echo get_template_directory_uri(); ?>'/bat/MailHandler.php'

Note -

By The Way, you can not use the Wordpress functions outside, unless you import wp-blog-header.php -

Example -

share|improve this answer
Yes that method works. But script is somewhat long and I prefer an alternative method.. – Upeksha Aug 27 '12 at 17:08

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.