How to Add Javascript in a WordPress Post or Page
Categories: Wordpress
One of my readers asked me the following question:
“How can I run a JavaScript within a WordPress post?”
In this short post I have explained how I use Javascript in my WordPress posts.
Adding javascript in WordPress post is really simple. You just have to switch to the HTML view in the editor and paste the JavaScript code and that’s pretty much it.
![]()
If the JavaScript code is in an external file then you need to include it first. You will need to add a bit of code similar to the following in your “header.php” or “footer.php” file to include the external JavaScript file:
I prefer to add javascript in the footer for site performance factors. You can paste it in the HTML view of the post too if this file is only needed in that one post. Replace “http://www.your-domain-name.com/scripts/my-javascript.js” with the actual location of the JavaScript file.
After you have included the file you can use any functions that is defined in that file. Use something similar to the following:
<!–
custom_function();
//–></script>
Replace “custom_function();” with the actual name of the function.
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Tags: Javascript, Wordpress









#1 by Arly on December 26, 2009 - 12:24 pm
I want to run the JS only on spesific page only, do you have any idea how?
Arly´s last blog ..GHR1000 Tips: How to Maximize Your Benefits While Using GHR1000
#2 by admin on December 26, 2009 - 11:35 pm
You can just put the javascript on those specific pages.
#3 by cool on January 9, 2010 - 1:32 am
ok, just remove line breaks from your js code.
from:
” js code line1;
js code line 2;
js code line 3;”
to:
” js code line1; js code line 2; js code line 3;”
#4 by Rav on January 23, 2010 - 2:44 pm
im trying to add this function on my wordpress page- its a click to reveal funcition as shown on the link below. Tried everything, cant seem to get it to work on a wordpress page, any ideas?
http://www.nisusnewmedia.com/reveal/
#5 by Mark D on May 29, 2010 - 10:24 am
Do you know if it’s possible to use jQuery inside a post? I mean if I wanted to use some of jQuery UI functionality would it be possible?
Also it’s worth mentioning that some ad networks are now trying to get you to use javascript inline when you post. What are your thoughts on that?
Mark D´s last blog ..Summer ready
#6 by admin on May 30, 2010 - 8:47 am
Yeah you can use JQuery inside posts too. As long as you have included the JQuery library in the page you should be fine.