Using Javascript

Javascript will work within WordPress. If used within the template files, most Javascript will work fine. Using them within a post is another matter, though.

Using Javascript

Javascript will work within WordPress. If used within the template files, most Javascript will work fine. Using them within a post is another matter, though.

Once you enter the world of PHP, it is really hard to go back to using Javascript, but they still serve their purpose. If you can replace a Javascript with PHP code, tags, or script in WordPress, do so. Your life will be much easier. If not, here are a few tips to make your Javascript work in WordPress.

Javascript in Template Files

This page is somewhat misleading. See this function page for better info:

To use Javascript repeatedly within your site, you can either set the call for the Javascript, or the script itself, in the head of your header.php template file, between the meta tags and the style sheet link, no differently than you would if you were using Javascript in any HTML page. To “load” the Javascript file into your site, in the head, add something like this:

If your custom Javascript is not working after including the previous line of code in your header.php template file, use the following line of code.

include the leading forward slash “/” even if your file is located at the root of your theme.

Read More: Editing Files

Be sure that you define the type correctly, as your site will not validate without it.

In the spot where you wish to use the Javascript, set the call for the Javascript. For example, you are using Javascript that sets a link for users to “email this page” to a friend and you want it to be under the post title. It might look like this: