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 6

  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

Post a Comment

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