Plugin / Script Logic

Tahir Yasin

Description

Description

This plugin lists all JavaScripts and Style sheets with a control field that lets you control CSS & JavaScript files to include only on the pages where you actually need them. The text field lets you use WP’s Conditional Tags, or any general PHP code.

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.)

The Basics

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 Y is TRUE when either X is true or Y is true.
  • && (AND) to make conditions more specific. X AND Y is 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.

Examples

  • 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.

Ratings

3
2 reviews

Rating breakdown

Details Information

Version

0.3

First Released

13 Sep, 2016

Total Downloads

1,370

Wordpress Version

2.8 or higher

Tested up to:

3.9.29

Require PHP Version:

-

Tags

Contributors

Languages

The plugin hasn't been transalated in any language other than English.

DIRECTORY DISCLAIMER

The information provided in this THEME/PLUGIN DIRECTORY is made available for information purposes only, and intended to serve as a resource to enable visitors to select a relevant theme or plugin. wpSocket gives no warranty of any kind, express or implied with regard to the information, including without limitation any warranty that the particular theme or plugin that you select is qualified on your situation.

The information in the individual theme or plugin displayed in the Directory is provided by the owners and contributors themselves. wpSocket gives no warranty as to the accuracy of the information and will not be liable to you for any loss or damage suffered by you as a consequence of your reliance on the information.

Links to respective sites are offered to assist in accessing additional information. The links may be outdated or broken. Connect to outside sites at your own risk. The Theme/Plugin Directory does not endorse the content or accuracy of any listing or external website.

While information is made available, no guarantee is given that the details provided are correct, complete or up-to-date.

wpSocket is not related to the theme or plugin, and also not responsible and expressly disclaims all liability for, damages of any kind, arising out of the use, reference to, or reliance on, any information or business listed throughout our site.

Keep Leading Your Followers!
Share it for them.