Javascript inside WordPress posts

WordPress doesn’t actually allow you to insert any code into post, without some external plugin’s help. Sure, you can put some html and css in there but even that i’ve found has somewhat unsatisfactory results.

Anyway, according to the WP site, to insert javascript you basically have 3 options:

  1. For jscript code appearing on all pages: Insert the code into your header.php file – then call up the code from within your post.
  2. For jscript code appearing on few pages: Insert all the code into a file called example.js – then combine both the call to the script file with the call to the Javascript function itself.
  3. For multiple scripts: Insert all functions into a file called allthecode.js – then follow either method 1 or 2.

I have already experimented with this a little..
I found that using a php include statement is a simpler option though
( example: <?php include ('allthecode.js'); ?> ).

Comments 10

  1. Den wrote:

    exactly, I tried to add js codes and causing problem with my layout and sidebar widget all over the place

    Posted 30 Sep 2007 at 10:55 am
  2. admin wrote:

    hi Den,

    there may very well be css code inside the js that is causing problems and messing up your design..

    try and “view source” in your browser and see if you can tell what code was added by the js script..
    if you see css style or new html elements in addition to javascript code then you need to organize your layout to anticipate this extra code.

    Posted 02 Oct 2007 at 2:42 pm
  3. apiz wrote:

    hye admin..may i ask u..how to post javascript code like u did above with the green font?
    please reply back to my email :) i already try using but it wont work...it will execute the javascript code..

    Posted 01 Mar 2008 at 2:12 pm
  4. elran wrote:

    hi apiz,

    if you just want to display code like i have in green above – then you could install a plugin called: Code Snippet.

    it will let you put different kinds of code in a wordpress post – but will display it as text – and will not execute it.

    Posted 01 Mar 2008 at 6:55 pm
  5. remove ipad scratch wrote:

    tried it out but everything went haywire. is there not a simple plugin that deals with this?

    Posted 13 Jun 2010 at 9:20 am
  6. iPhonulous | Hacking wrote:

    Works to some extent, I also do notice some weird layout glitches :/

    Posted 28 Jul 2010 at 5:08 pm
  7. Chauncey Louvier wrote:

    My wife and i got really excited that Albert managed to finish up his analysis from the precious recommendations he got through the web site. It’s not at all simplistic to simply choose to be handing out solutions that a number of people may have been making money from. We grasp we have got the writer to thank because of that. The specific illustrations you have made, the simple website navigation, the friendships you will assist to instill – it’s mostly amazing, and it’s assisting our son and us recognize that the theme is cool, and that is extremely pressing. Thanks for the whole thing!

    Posted 13 Jun 2012 at 12:46 pm
  8. victor spano wrote:

    Don’t waste energy on topics that don’t arouse your senses; this just slows you down and creates problems with your long term strategy.

    Posted 15 Jun 2015 at 2:35 am
  9. lloyd segal wrote:

    We are a successful software design and development company specializing in creating e-commerce Websites, iPhone, Android, and windows 8 applications, facebook applications, Internet marketing (SEO/SEM/SMO and PPC campaigns)

    Posted 13 Jul 2015 at 3:54 am
  10. Nova Merchant Services wrote:

    Interesting topic but I’m not sure that I agree with you. Things like this have chanbged the way the web was formed and created. Is it really as simple as changing out the direction and watching the web change?

    Posted 25 Oct 2015 at 3:14 pm

Post a Comment

Your email is never published nor shared. Required fields are marked *