Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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,

mailHandlerURL:'bat/MailHandler.php'

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
add comment

2 Answers

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
add comment

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

Example -

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

Note -

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

Example -

require_once('./blog/wp-blog-header.php');`
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
add comment

Your Answer

 
discard

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.