NOTE: The script logic you introduce is evaluated directly. Anyone who has permission to manage options will have the right to add any code, including malicious and possibly destructive code. There is an optional filter ‘script_logic_eval_override’ which you can use to bypass the EVAL with your own code if needed. (See Other Notes).
Writing Logic Code
The text in the ‘Logic’ field can be full PHP code and should return ‘true’ when you need the script to load. If there is no ‘return’ in the text, an implicit ‘return’ is added to the start and a ‘;’ is added on the end. (This is just to make single statements like is_home() more convenient.)
Make good use of WP’s own conditional tags. You can vary and combine code using:
!(NOT) to reverse the logic, eg
!is_home()is TRUE when this is NOT the home page.
||(OR) to combine conditions.
X OR Yis TRUE when either X is true or Y is true.
&&(AND) to make conditions more specific.
X AND Yis TRUE when both X is true and Y is true.
There are lots of great code examples on the WP forums, and on WP sites across the net. But the WP Codex is also full of good examples to adapt, such as Test if post is in a descendent category.
is_home()— just the main blog page
!is_page('about')— everywhere EXCEPT this specific WP ‘page’
!is_user_logged_in()— shown when a user is not logged in
is_category(array(5,9,10,11))— category page of one of the given category IDs
is_single() && in_category('baked-goods')— single post that’s in the category with this slug
strpos($_SERVER['HTTP_REFERER'], "google.com")!=false— script to load when clicked through from a google search
is_category() && in_array($cat, get_term_children( 5, 'category'))— category page that’s a descendent of category 5
global $post; return (in_array(77,get_post_ancestors($post)));— WP page that is a child of page 77
global $post; return (is_page('home') || ($post->post_parent=="13"));— home page OR the page that’s a child of page 13
Note the extra ‘;’ on the end where there is an explicit ‘return’.
The ‘script_logic_eval_override’ filter
Before the Script Logic code is evaluated for each script, the text of the Script Logic code is passed through this filter. If the filter returns a BOOLEAN result, this is used instead to determine if the script should load. Return TRUE to load.
- Upload plugin to the
- Activate the plugin through the ‘Plugins’ menu in WordPress
- Configure the plugin at
Settings -> Script Logic
- That’s it.
On plugin configuration page you see all script and CSS files with a logic field. Lets suppose you are using Contact Form 7 plugin. You created a page Contact Us (slug = contact-us) and want to include Contact Form 7 scripts only on this page. Find the Contact Form 7 scripts on plugin’s configuration page (they have handle = contact-form-7) and put
is_page('contact-us') in logic field
- Why isn’t it working?
Try switching to the WP default theme – if the problem goes away, there is something specific to your theme that may be interfering with the WP conditional tags.
Most probably the logic text on one of your scripts is invalid PHP
- How do I load a certain script X on just my ‘home’ page? (Or on every page except that.)
There is some confusion between the Main Page and the front page. If you want a script on your ‘front page’ whether that is a static page or a set of posts, use is_front_page(). If it is a page using is_page(x) does not work. If your ‘front page’ is a page and not a series of posts, you can still use is_home() to include scripts on that main posts page (as defined in Admin > Settings > Reading).
There are no reviews for this plugin.
Contributors and Developers
“Script Logic” is open source software. The following people have contributed to this plugin.Contributors
Translate “Script Logic” into your language.
Interested in development?
Browse the code, check out the SVN repository, or subscribe to the development log by RSS.
- First stable release.