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):
Here’s what the meta box looks like on the Write Post/Page 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
- Download Version 0.1 (Downloaded 1405 times.)
- Support Questions
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.


Great idea. As I rebuild my site site from the ground up I’ve thought more and more about performance issues regarding http requests. My idea was to use regex to look for a specific string in the results instead of the wp conditionals.
For example, parse the output for rel=”lightbox” to see whether or not the js is needed. This would pretty much guarantee that you’re only loading what you need.
I’m not much of a programmer so I’m not sure what is accessible before the loop fires for this sort of parsing.
“I’m not much of a programmer so I’m not sure what is accessible before the loop fires for this sort of parsing.” I think content above is clear .
I think pretty much anything you need is accessible before the Loop with this.
For example, I’m using the
has_tag('gallery')to run the check on single posts because I’m loading Thickbox for my posts tagged Gallery.You should be able to use any of the WP conditional tags with this. I haven’t ran every check imaginable though.
Hey! Thanks for this brilliant idea! I have been looking for a way to do this for months!
However, I could not get it to work at all. Tried inserting link to js from assorted plugins, then tried conditional tags: is_page(pageID), is_page(array(pageID, pageID)), comment_open() and a few others… none worked.
Man… if you could get this working…genius!
My only suggestion — other than making it work
— is that it would seem much easier and useful in certain cases to be able to exclude the js, rather than include it. For instance: If we have a script that gets called on every page except for one. Know what I mean?
Thanks!!!!
Conditional tags are based on a condition (true or false), so you can exclude/include in any way you want.
And, of course, the plugin already works. I’m using it here on this site.
“Conditional tags are based on a condition (true or false), so you can exclude/include in any way you want.”
—————–
Ok. I understand that. But your instructions are not altogether clear — because you say not to use <?php tags, but don’t mention anything about ‘if’, ‘else’, etc… and when I tried adding a full conditional statement, with ‘if’s’ and such (but without <?php tags) I got an error. Also, when inserting a link to the .js, should it be a full path, including http:// ?
Perhaps you could clarify, by example, how I would include a script such as cforms.js only on pages with open commenting
comment_open()— OR how would you exclude that same script from only index.php?Thanks.
So, if I understand correctly, you are asking me (and others) to pay $25 to ask you a question (on a forum that currently has not a single question/topic (that I could find) on even relating to this plugin), about a 5 day old plugin that came with minimal instructions, which you yourself state on this page “is an early beta release, so it’s not fully developed. There’s the likelihood of a bug here or there”?
I certainly appreciate your hard work on this plugin, and as I stated in my 1st comment, it is greatly desired/needed. And I absolutley understand your need to earn a living for the time and hard work you put in. But I think what you are asking for is out of line for a 5 day old, ‘not fully developed beta release’.
Very disapointed.
Good luck with this.
Circle Up
You are certainly welcome to use the WP.org support forums where you can get free support. I just don’t answer support questions here in the comments or by email. Thus far, you seem to be the only person that can’t get the plugin to work correctly. If I had a major flood of bug reports from other users here, I’d do a little more digging.
The more money I’m paid, the more time I have for development. Otherwise, I’d have to get a real job, which would give me less time on developing plugins and themes. That would also give me even less time for support. That’s simply how it works.
All of my users know how I handle support, and I’m guessing you’re new around these parts. You are welcome to sign up for a small, yearly fee to ask support questions on all of my themes and plugins. My $25 fee is nothing compared to what a lot of other people are charging, and most users get much more than $25 worth of my time in an entire year, which I’m happy to give.
Finally! I’ve been waiting to find something regarding superfluous script loading from WP plugins. It’s a shame to see so much waste with plugin scripts. With 100 pages but only 2 that require a lightbox or post tabs…very cool. This is possibly one of the most important plugin concepts I’ve seen all year.
Even though im not that much into coding and programming, but i do design wordpress themes, which gets coded by one of my friends and we quite a lot use JS resources, This plugin would be really great for us. Thanks mate.
Hi Justin,
First of all, just want to say that I appreciate all the plugins and themes that you’ve done.
Now onto a feature request, I think that JavaScript Logic could benefit from detecting a theme’s javascript folder.
Kind of a how you have the select menu for internal Wordpress scripts and plugin scripts, a similar select menu can be done for the proposed theme javascript folder.
The theme’s javascript folder could be defined as an input text field that the user defines somewhere in JL’s options.
What do you think?
-Ray
Also, have you thought of creating a sister plugin for stylesheets called “CSS Logic”?
I was also unable to get this plugin to work on 2.7.1 Either using the plugin options page or the page specific fields. All js loaded on every page, even with simple test is_home() and full js uri in the script field. I’m wandering if this could be a theme specific problem? If you get a moment to pass by the wordpress forums you’ll see my post there about this. I can’t believe that Ive inputed the details wrong It’s so simple and $25 is a lot to find out my theme functions are just not compatible … The concept is desperately overdue however and I applaud your efforts to optimize wordpress. The headspace plugin tries to do something similar, but also fails if plugins don’t follow a certain design. The whole WP plugin archtecture needs an overhaul to standardise the way scripts are called and prevent so much dead weight being carried around.
I add this plugin and I place javascript url (http://70.107.232.161/wptest1/post-wptest1.js) in Javascript logic setting box on Write Post/Page page. But it’s doesn’t seem working. I see the source of page and javascript was not linked. Do I need to do something eles? Doyou have a tutorial for beginner?
Really good! I add the plugin and it is working perfectly.
Thank
I have tried your plugin and i must admit this is what im looking for all this while. It saved me hours just to figure out how to add JavaScript in however i want.
I used this plugins together with custom fields for css, the “tag” way is awesome!.
Thanks again :*