Script Logic Plugin

Script Logic Plugin - Conditionally load JS and CSS files in WordPress pages

Introduction

Script Logic is a WordPress plugin that lets you control on which pages scripts and style sheets load using WP’s conditional tags.

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

Installation

  1. Upload plugin to the `/wp-content/plugins/` directory
  2. Activate the plugin through the ‘Plugins’ menu in WordPress
  3. Configure the plugin at `Settings -> Script Logic`
  4. That’s it.

Configuration

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

 

Usage Example

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.

Step 1:
Find the Contact Form 7 JS and CSS on plugin’s configuration page (they have handle = contact-form-7).
Step 2:
Put is_page('contact-us') in logic field

Script Logic Plugin - Load contact form 7 JS files only on contact page

Script Logic Plugin - Load contact form 7 CSS files only on contact page

Step 3:
Dance! You just excluded 2 files from all pages except your contact us page making your site load faster!!!

 

Author: Tahir Yasin

Tahir is a Passionate Web Developer from Lahore, Pakistan and a guy with NEVER-STOP-LEARNING attitude who keeps an eye on emerging technologies and customer trends.

15 thoughts on “Script Logic Plugin”

  1. This is the one of the plugins i always wanted. Thanks to Tahir Yasin that he put effort to create this plugin. Awesome release. Keep it up!

  2. I think this is аmong thе most vital info for me. And i am glad reading your article.
    But annа rremɑrk on some general things, The ԝebsite style is perfect, the articles is rеally nice :
    D. Good job, cheers

    1. PHP (Hypertext Pre Processor) is a server side web scripting language used for developing websites.

      Hosting servers are used to host websites. This is where your source code files reside.

      FTP (File Transfer Protocol) is used for transferring files.

      SQL (Structured Query Language) is used to communicate with databases.

      Plugins are little piece of software used to extend functionality of an existing software system.

  3. A Big Thanks to Tahir!! ( Great helping hand to figure things out !)
    You the man 🙂 !!
    Everything works flawlessly.
    Thanks…
    Farhan

  4. Hi it is giving me an error saying

    Fatal error: Unsupported operand types in /wp-content/plugins/script-logic/script-logic.php on line 155

    do let me know what the issue can be

    1. Replace the line#155

      $merged_queue = $sl_settings->queue + $wp_scripts->queue;
      

      with

      if(is_array($sl_settings->queue))
          $merged_queue = $sl_settings->queue + $wp_scripts->queue;
      else
          $merged_queue = $wp_scripts->queue;
      

      And please let me know if this solves your issue?

  5. Hi nice plugin mate. But I am facing an issue. I am using Coupon Code Lite plugin to display coupons on my WordPress site but it loads the .js file on every page. As suggested above, I have added the is_page(‘deals-2’) condition to load it only on deals page. But even after that, the file is being loaded on all the pages that I checked using GTMetrix and Google Page Speed analyzer. Any idea ?

    1. Bilal, Thanks for liking the plugin.

      What is the name of .js file that you couldn’t control with the plugin? Be informed that you can control all JS and CSS files that were included using wp_enqueue_script and wp_enqueue_style functions.

      If you are adding script or style tags directly in header/footer then this plugin will not control them.

Leave a Reply

Your email address will not be published. Required fields are marked *