How to speed up WordPress by selectively disabling/enabling plugins

Recently when running performance tests using P3 (Plugin Performance Profiler) I noticed that plugins were running on pages that they didn’t need to – adding unnecessary load time to pages.

In my case I was seeing Gravity Forms using 41% of the server runtime – on a page that didn’t have anything to do with Gravity Forms.

WordPress-LimitPlugins1

The solution — use the option_active_plugins filter to control which plugins are loaded.

In this case I wanted to make the Gravity Forms plugin only run on the ‘contact’ page and in the WordPress administration (wp-admin).

The code below shows how to do this. You will need to run it from the mu-plugins folder (mu means Must Use – and it’s loaded early in the loop). The mu-plugins folder should be sitting under wp-content/mu-plugins – if it doesnt exist you will need to create it.

To configure you’ll need a basic understanding of PHP to define which pages you either do or don’t want plugins to load in and list the plugin path.

The difference for my website was significant, well worth implementing.

WordPress-LimitPlugins2

Download: itsg-limit-plugins.zip

<?php
/*
Plugin Name: itsupportguides.com limit plugins
Description: Limit plugins from running on specified pages.
Version: 1.0.0
Author: IT Support Guides
Author URI: itsupportguides.com
*/

add_filter( 'option_active_plugins', 'itsg_limit_plugins' );

 function itsg_limit_plugins( $plugins ){
    $plugins_not_needed = array();
    
    $page = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );

    if( $page !== '/contact/' && !is_admin() ) {
        array_push($plugins_not_needed,
            'gravityforms/gravityforms.php' // Gravity Forms
        );
    } 
          
     foreach ( $plugins_not_needed as $plugin ) {
         $key = array_search( $plugin, $plugins );
         if ( false !== $key ) {
         
            unset( $plugins[ $key ] );
         }
     }
     
     return $plugins;
 }