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.
Authors, photographers, teachers, and public speakers, Lorelle and Brent VanFossen, travel extensively with their camera and pen in hand to bring you a variety of articles on nature and travel photography including basic nature photography and the photography business, writing, travel, recreational vehicles, web page design, and life on the road. All images, design, and content are copyrighted and protected by law.
You can find related articles to this topic in our WordPress and Photography categories. The previous post is A New Kind of 404 Page Not Found Result and the next post is Translation for Your Blog in Eight Languages. Using Javascript In WordPress, Issue Number 654, by Lorelle VanFossen, was updated June 14th, 2005.
You can follow comments through the RSS 2.0 feed and you can find more feeds on our Feeds List. Your comment is welcome, as are trackbacks from your site.
Related Articles
- WordPress Press It Features
- List of WordPress Plugins
- Changing the WordPress Quicktag Buttons
- Blogpoly: The New Monopoly for Bloggers
- Invitation to Hebrew and Arabic Bloggers to Blog for Free on Wordpress.com
- WordPress Plugins Database
- WordPress Codex Cleanup Week
- WordPress Backup Week July 23-30 - Get Backed Up
Submit Article: BlinkList | Blogmarks | Digg | Del.icio.us | Ekstreme Socializer | Feedmarker | Furl | Google Bookmarks | ma.gnolia | Netvouz | RawSugar | Reddit | Scuttle | Shadows | Simpy | Spurl | Technorati | Unalog | Wink | Yahoo MyWeb2


One reply. Want to add more?
RSS feed for comments on this post. TrackBack URIPeople are talking here
Trackbacks and Pingbacks: What People are Saying
[…] 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 […]
Pingback by Lorelle on WordPress » Using Javascript in WordPress — September 17, 2005 @ 16:17