Disable plugins in specific URLs in WordPress

Some WordPress plugins can be useful or necessary for most of your pages in your site, but not for all of them.

Usually, there is no problem, they are just unused in some pages. But, in case your plugin is adding overhead to your site you may want to disable it in some pages.

This way you can reduce the time of the page to respond (Time To First Bit) preventing some plugin PHP execution, or some CSS and JS to be added, improving your frontend performance.

It is not complicated to selectively disable plugins in your site. You just need to create a few lines mu-plugin, as simple as:

<?php
/**
* Plugin disabler for specific URLs
*
* @package Timeular
* @author Julià Mestieri
* @version 0.1.0
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License version 2 (GPLv2)
*/
namespace YourNamespace;
/**
* Class Plugins_Disabler.
*/
class Plugins_Disabler {
/**
* Maps the plugins to disable in a given URL regex match.
*
* @var array
*/
private static $url_plugin_map = [
'#^/checkout#' => [ 'js_composer/js_composer.php' ],
'#^/cart#' => [ 'js_composer/js_composer.php' ],
];
/**
* Initialize the plugin behavior.
*
* @param array $plugins Plugins enabled in the site.
*/
public static function init( $plugins ) {
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
return $plugins;
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
$check_uri = rtrim( strtolower( sanitize_text_field( $_SERVER['REQUEST_URI'] ) ), '/\\' );
foreach ( self::$url_plugin_map as $regex => $disable_plugins ) {
if ( preg_match( $regex, $check_uri ) ) {
foreach ( $disable_plugins as $plugin ) {
$key = array_search( $plugin, $plugins, true );
if ( false !== $key ) {
unset( $plugins[ $key ] );
}
}
}
}
return $plugins;
}
}
add_filter( 'option_active_plugins', [ '\YourNamespace\Plugins_Disabler', 'init' ], 99 );

In this case, we are disabling js_composer in the checkout pages.

Let’s look into it in-depth:

/**
 * Maps the plugins to disable in a given URL regex match.
 *
 * @var array
 */
private static $url_plugin_map = [
  '#^/checkout#' => [ 'js_composer/js_composer.php' ],
  '#^/cart#'     => [ 'js_composer/js_composer.php' ],
];

Here it defines which plugins we want to disable and in which URL’s. It defines a key-value array with the URL RegEx to match as key, and an array of plugins to disable in the given URL as array value. Note that array value is an array that can contain multiple plugins to disable. Also important és that the plugin name should be in the internal WordPress format folder ( js_composer )+ main php file name ( js_composer.php ).

Then it has the main function that we are going to hook. Let’s look into it:

if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
  return $plugins;
}

It uses $_SERVER['REQUEST_URI'] so, in case it is not defined it skip all the function.

$check_uri = rtrim( strtolower( sanitize_text_field( $_SERVER['REQUEST_URI'] ) ), '/\\' );
foreach ( self::$url_plugin_map as $regex => $disable_plugins ) {
  if ( preg_match( $regex, $check_uri ) ) {
    foreach ( $disable_plugins as $plugin ) {
      $key = array_search( $plugin, $plugins, true );
      if ( false !== $key ) {
        unset( $plugins[ $key ] );
      }
    }
  }
}
return $plugins;

This is the core of the thing. Uses the mapping defined in self::$url_plugin_map to check if URL regex, and in case it math removes all the defined plugins. The function parameter $plugins is the list of all the plugins enabled, so with the unset( $plugins[ $key ] ); it removes the given plugin name from the list, with the same result of the plugin disabled, but only applying in the given URLs

Finally, it hooks it in the proper filter:

add_filter( 'option_active_plugins', [ '\YourNamespace\Plugins_Disabler', 'init' ], 99 );

Note that option_active_plugins is a specific case of option_{$option} as defined in WordPress docs: https://developer.wordpress.org/reference/hooks/option_option/

The option hooked is the plugins enabled, after loading it and before it proceeds to load the plugins.

Do you like this solution? does it help to improve the performance of your WordPress site? Do you know alternative strategies to achieve it? Share your knowledge!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: