How to add JavaScript to WordPress posts and pages

0 Flares 0 Flares ×

Adding JavaScript to your entire wordpress site can be easily achieved by modifying your theme files but there may be some instances where you need to add JavaScript to some specific pages and posts. So how is it possible? If you are using the self hosted wordpress then this is easily achieved by toggling the writing mode from Visual mode to Html mode and inserting the JavaScript into the text box.


How to add JavaScript to WordPress posts and pages


An alternative way to add JavaScript code to wordpress


If directly adding JavaScript code to the page or post does not work for you then you can use this alternate way to add JavaScript code to wordpress pages or posts. This option is a better way of adding JavaScript code to your posts, here we use a wordpress plugin, Custom Field Short code. The idea behind this is that you create a custom field and set the JavaScript as the value of this custom field and add the custom field using short code to your post or page. This way of adding JavaScript makes it easy to maintain in future. The idea of using Custom Field Short code is that, easily you can insert the custom field into your post just by adding a short code.


So lets see the complete process of adding JavaScript to wordpress post:


1. Install Custom Field Short code plugin.

2. Create a custom filed while creating the new post, lets say the name of your custom field is “myjs” and set the JavaScript as the value for this custom field. (Note if you are using wordpress 3.1+ you will not be able to see the custom field option in the new post as by default it is disabled. If you do not see the custom field editor then click on the Screen Options and select the check box that says “Custom Fields”)


    How to add javascript to WordPress


3. Now toggle to HTML mode in your post editor and insert the custom field as short code that is [cf]myjs[/cf]. Save and publish it, when you open your post, wordpress will automatically replace the short code with the JavaScript.


    Add Custom field javascript to WordPress



So these are the ways of adding javascipt to any wordpress posts or pages without the need for modifying your theme files.


+Ayodhyanath Guru holds a B.Tech degree in Electrical Engineering and has worked with various prestigious clients in the IT industry and presently working as a Software Engineer. He is a part time blogger and presently authors the Jafaloo.Com blog. Being a tech enthusiast Guru likes to surf the web and blogs about interesting technical topics like How-To guides, freewares, Tutorials, Software, Gadgets, web applications etc. Apart from blogging he likes coding in Java/J2EE and PHP.

You may also like...

2 Responses

  1. Growingace Shaheen says:


  2. Prince says:

    I tried all steps still it isn’t working…

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 0 Flares ×