Justin Tadlock

JavaScript Logic: WordPress Plugin

One of my biggest gripes about many plugins (and even some themes) is how JavaScript is handled. Many times, it’s loaded on every page of your site without the need for it.

There’s also potential conflicts between different JavaScript libraries and other scripts. It can be problematic having many different scripts loading at once.

This plugin allows you to conditionally load JavaScript files. I stole the idea from Widget Logic (which is an awesome plugin).

How to use this plugin

I’ve included a full readme.html, so I wouldn’t have to add too many instructions to this post. To conditionally include JavaScript, you need to understand how WordPress conditional tags work. There is no other way around that. Or, you can load it on a post-by-post or page-by-page basis with an additional meta box.

You can load many of the scripts packaged with WordPress, those included with the plugin, or an external JavaScript file.

Here’s what the plugin settings page will look like (click image for larger view):

WordPress Plugin: JavaScript Logic Settings

Here’s what the meta box looks like on the Write Post/Page screen:

JS Logic on the Write Post Screen

Some things to note about this plugin

This is an early beta release, so it’s not fully developed. There’s the likelihood of a bug here or there. All files selected in each JavaScript set will be uploaded (choosing one won’t overwrite the others). The plugin is now out of beta.

The settings page will also be located under Appearance > JavaScript Logic. Or, you will see an extra meta box on the Write Post/Page screen for loading JavaScript on individual posts or pages.

If you want to see it in action, notice that I’m conditionally including thickbox with has_tag('gallery') on the single view of posts with the Gallery tag.

If you have ideas, I’m certainly open to hearing them.

Download the JavaScript Logic plugin

Please don’t use my contact page or the comments section below to ask support questions. Use my support forums at Theme Hybrid, which is where I handle all support questions for my WordPress projects.

Feel free to report any bugs you come across or share ideas you have about making the plugin better though.