A guide to JavaScript Logic

JavaScript Logic is a plugin that allows you to add JavaScript however you want. There's no need to add a script on all of your blog's pages when you only need it on a few.

By using WordPress conditional tags or through a meta box on the Write Post/Page screen, you control what files are added to your site and when.

How to install the plugin

  1. Uzip the javascript-logic.zip folder.
  2. Upload the javascript-logic folder to your /wp-content/plugins directory.
  3. In your WordPress dashboard, head over to the Plugins section.
  4. Activate JavaScript Logic.

How to use the plugin

Once you've activated the plugin, you have two choice of how to add JavaScript:

JavaScript Logic Settings

Go to the JavaScript Logic page located under Appearance (maybe Themes or Design, depending on what WP version you're using). You can have site-wide control over when JavaScript is loaded from this page.

There are 10 JavaScript Sets altogether. You can add up to three different scripts to each set. I'll walk you through what each setting means here.

Select WordPress Script:
This section allows you to include a JavaScript file that is packaged with WordPress. You can use the wp_enqueue_script() page for reference.
Select Plugin Script:
These are scripts that are included within the plugin package. You can find these in /javascript-loader/js.
Custom File URL:
You can input a URL to any JavaScript file you have on your site.
Script Logic:
Use WordPress conditional tags to tell the script when to load. Do not wrap it with <?php and ?> tags. If nothing is input, your JavaScript will load on every page (see examples below).

Here's an example of Script Logic that loads a set on the home page.

is_home()

An example of loading scripts on the About page or a post with the ID of 205.

is_page('about') || is_single(205)

How about we get a little more complicated? Let's check for single posts with the tag of Gallery, the category archive WordPress, or all search results pages.

has_tag('gallery') || is_category('wordpress') || is_search()

Okay, that wasn't really complicated. WordPress conditional tags are actually quite simple to use.

Write Post/Page Meta Box

This plugin will also provide you with an additional meta box on the Write Post and Page screens. It's clearly labeled JavaScript Logic Settings.

This will allow you to load JavaScript on a per-post or per-page basis. It's great because it gives you control right when you're writing a post/page, but it's somewhat limiting because you can't add as many scripts as you can from the plugin settings page.

There are three boxes. Anything you select or input will be automatically loaded on that particular single post or page view.

WP JavaScript:
This section allows you to include a JavaScript file that is packaged with WordPress. You can use the wp_enqueue_script() page for reference.
JS Logic Script:
These are scripts that are included within the plugin package. You can find these in /javascript-loader/js.
JavaScript URL:
You can input a URL to any JavaScript file you have on your site.

Plugin support

I run a WordPress community called Theme Hybrid, which is where I fully support all of my WordPress projects, including plugins. You can sign up for an account to get plugin support for a small yearly fee ($25 USD at the time of writing).

I know. I know. You might not want to pay for support, but just consider it a donation to the project. I am fully employed through my work with WordPress, so to continue making cool, GPL-licensed plugins and having the time to support them, I must pay the bills.

Copyright & license

JavaScript Logic is licensed under the GNU General Public License, version 2 (GPL).

This plugin is copyrighted to Justin Tadlock.

2008 © Justin Tadlock