Google
 
Web life.currenttoday.com
currenttoday.com

Very Easy Javascript in a WordPress Post

The other day I wanted to enter a bit of javascript on one of my posts. I was making a BMI calculator page.

Well, I followed my first stupid inclination, and started typing javascript tags with code and form tags directly into the post editor in WordPress. You guessed it. That didn’t work.

After snooping around a bit, I started to rework the javascript code I already had in PHP. PHP is much easier to integrate with WordPress.

Well, I’m the kind of programmer who cuts and pastes everything I can. I haven’t mastered any given language. So, anytime I need to do something, I search for examples to get me started. If you can program in one language, all you need is a good example to get you going in another language [usually].

Many of you WordPress and Javascript gurus may think this is a stupid simplification or just plain unnecessary. But I’m a rather lazy person with a natural tendency to find the path of least resistance.

Forgive me if this is common knowledge in WordPress using javascript. But, I hadn’t gotten very far when it hit me that there was another really easy, very lazy, yet rather exotic way to put javascript into a WordPress post that would allow site wide editing. So I’m sharing it even if you gurus already know it.

If you have used WordPress at all, you likely are familiar with comments that look like this:

<!—adsense—>

Well, look at this one:

<!—adsense#Clock—>

Now look what it does:

That’s right! It hit me that the common plugin, Adsense-Deluxe, was already allowing the Google ads to use javascript anywhere you placed them in WordPress.

He he. The lazy man is a superior man.

Again, forgive me if using Adsense-Deluxe to get javascript into WordPress posts easily is old news.

If you want to go just a little further all you have to do to include javascript in the traditional way is to save a file with your javascript in it and place it in your post like this:

<script type=“text/javascript” src=“/scripts/myscript.js”></script>
<script type=“text/javascript”>
<!–
myfunctioninthescript();
//–></script>
 

If the script just runs as a whole, then you don’t need to call the function.

Now, there is a third way to get javascript into a WordPress post easily. It can have some issues if you aren’t careful, though.

Simply echo your script where you want it using PHP.

But, we were looking for an easy way to do it. Weren’t we?

One other way is an iframe tag. You can see my sudoku page doing that by Clicking Here.

Regards.

One Response to “Very Easy Javascript in a WordPress Post”

  1. Blogging for fun. Blogging for money. Blogging for fun, but making money. Says:

    […] In March, I spent a lot of effort learning my way around Wordpress. I had to know how to do PHP and especially Javascript in a WordPress post. I first used javascript in a post making a BMI weight calculator. Then, I shared how to easily get javascript in a Wordpress Post. […]