If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

OK. Enough. I’ve had it. How can WordPress not allow built-in Javascript in your post content. And more importantly, how come no-one has released a simple plugin to allow it?

Well, someone has.

Me.

Today.

It took about 5 minutes to write, after 2 hours of messing around with various alternative plugins, trying to hack them to work.

Download The Plugin
Click here to download the plugin.

Installation Instructions
1. Download the plugin
2. Unzip it
3. Copy the one file contained therein to your wp-content/plugins directory
4. Log into WordPress. Go to Plugins. Enable the plugin
5. Insert the javascript in your post.

Upgrade Instructions
1. Download the plugin
2. Unzip it
3. Copy the one file contained therein to your wp-content/plugins directory

That’s it.

WordPress Version Compatability
WP 2.7 - Version 1.0, 1.1 of the plugin works OK
WP 2.3.3 - Version 1.0, 1.1 of the plugin works OK
WP 2.3.2 - Version 1.0, 1.1 of the plugin works OK
WP 2.0.3 - Wordpress.com don’t keep a copy of this archive, so I can’t test this version.

Comments
I’m using the simple text editor in my posts, by disabling the WYSIWYG editor in my Wordpress profile. (See Users->Your Profile - uncheck the box marked ‘Use the visual editor when writing’)

I’ve not tried it with the visual editor, as I never use it anyway.

Update (18th July 2007): The WYSYWIG editor definitely breaks it when you edit the post (It puts a backslash in front of any single-quotes). You can go into the “code” view and fix it manually, but you have to do that each time you edit the post.

Update (20th July 2007) : I’ve just tested the plugin with two inline javascript calls, and one call to an external script and it worked fine. Just one note: any JS called in the post will ALSO take effect when you edit the post, since it will run in the preview section.

Update (3rd January 2008): I’ve started the “WP version compatability” list above. Feel free to let me know if you have another version, and the plugin works or doesn’t. I’ll do my best to keep this list up to date.

Update (03 February 2009): Just release version 1.1, which fixed some issues with Javascript code formatted in multi-line statements. No idea how I missed this one, but thanks to the people who do actually add their sample JavaScript code to the comments on this post!

Example

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Live
  • Furl
  • StumbleUpon
  • TwitThis
  • YahooMyWeb

66 Responses to “Javascript In WordPress Posts”

  • van says:

    im still having a problem
    if i put my adsense javascript on post
    the browser doesnt stops loading it seems it hangs already.

    so i tried installing adsense insertion plugin
    and also cannot save my adsense code

    and search for javascript and landed on this page

    but stil after activating your plugin

    my javascript saving still does not complete

    hope someone could help me.

    thanks

    using wp2.7.1

  • Andrew says:

    Hi Yaap,

    OK, I’ll look into that one.

    Andy

  • Adam says:

    Thank you so much! I was loosing my mind with the most simple Javascript that was not working inside of Wordpress. I had no idea that WP somehow blocked Javascripts from executing without your plugin. What a relief - thanks!

  • ramesh says:

    Big Thanks to u…………….
    My head ache gone b’cos of ur plugin.
    It’s awesome.

  • Hi Guys,

    Installed the plugin but having a problem, it doesn’t render the javascript! Its a very simple script, and it appears in the code but not on the page.

    Any ideas? Here’s the link to the page:
    http://www.review-hostel.com/index.php/book-a-trip/

    Thanks for any help!

  • Andrew says:

    Hi,
    There are a couple of parameters at the end of the SRC for that code:

    pid=3476707&aid=10524311

    I’m guessing that should be

    pid=3476707&id=10524311

    I’ll see what I can find out.

    Andy

Leave a Reply