with Lorelle and Brent VanFossen

Using Javascript In WordPress

Once you enter the world of PHP, it’s really hard to go back to javascripts, 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.

First, if you can, take all the scripts and put them into individual or one group Javascript file. Even if you don’t use the scripts for every single page or post on your site, they can still just sit there until called.

Make sure that each script is defined by its function name such as:

function updatepage(){var m="Page updated "+document.lastMo.......}

Let’s assume that you called your group javascripts file scriptfile.js and it contains the updatepage script. When you want to use the example updatepage Javascript, there are two ways to include it in your WordPress site.

If you are using this on every single page within your site, in the header.php between the meta tags and the style sheet link in the head section, paste the following (changing the name to your script file name):

<script type="text/javascript" src="/scripts/updatepage.js"></script>

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

In the place on your index.php, single.php, sidebar.php, or whichever template file you want to activate the Javascript, place the following code in exactly this format, changing the name to the function call of the script:

<script type="text/javascript">
<!--
updatepage();
//--></script>

To include a Javascript inside a post, you need to combine both the call to the script file with the call to the Javascript itself.

<script type="text/javascript" src="/scripts/updatepage.js"></script>
<script type="text/javascript">
<!--
updatepage();
//--></script>

If you are having trouble with including javascripts inside a post, use the Text Control Plugin which allows you to control on a global or per post basis the ability to turn off WordPress’s automatic formatting features which can quickly turn a code into something readable instead of executable. Set the options on the post that you will be using the Javascript on to have No Formating and No Character Formating. As a reminder, when using the Text Control Plugin, you must first Save and Continue Editing the post in order to see the Text Control Plugin options.

Without formatting, your post’s text will all run together, so you will have to add paragraph tags and other HTML tags in order to format your page as WordPress normally does that for you.

If your Javascript doesn’t work, triple check that you haven’t made any errors during the cut and paste into a group or single file. Be sure you used a text editor and not a word processing program to create the Javascript file. Check the name of the function in the script file as well as on your site. Not all javascripts may work, and could possibly conflict with your PHP commands, but this is very rare. If you are really having trouble with this, the WordPress Support Forum may be able to help.

One Trackback

  • […] If you are using the full version of WordPress and having trouble with including a lot of javascripts inside a post, try using the Text Control Plugin. This allows you to control WordPress’ automatic formatting features on a global or per post basis the ability, turning them on and off. The WordPress automatic formatting features can quickly turn a code into something readable instead of executable. To use this plugin, the entire post must be setup in HTML tags using paragraph tags and such as they will be turned off by the lack of formatting, and set the plugin options on the post that you will be using the Javascript on to have No Formating and No Character Formating. As a reminder, when using the Text Control Plugin, you must first Save and Continue Editing the post in order to see the Text Control Plugin options. This is only to be used by the brave as it is a lot of extra work. technorati tags: blog administration, wordpress, javascript, wordpress tips, wordpress templates, wordpress help site search tags: blog administration, wordpress, wordpress.com, wordpress tips, wordpress templates, javascript, wordpress help © Reprinted with Permission […]

Post a Comment

Your email is kept private. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.