Skip to content

Selectively disable plugins on WordPress for a specific request

For last few days, I have been working on a WordPress project where I am creating a set of APIs to be consumed by mobile apps. While writing these REST APIs I have tried to make the things work faster. While checking the REQUEST/RESPONSE time, I found that the WordPress initialization is taking much time then the API method processing. I looked into the time taken for the whole flow through xDebug Profiler and discovered that there are lots of plugins which are not needed for the API processing and are consuming most part of the loading and API response timing. Plugins like Backup WordPress, Contact Form 7, Simple Responsive Slider, Google Site-map, FancyBox, WordPress SEO etc. are required for the website front-end and don’t really play any role in the APIs.selective-loading-wp-plugins

Now, my intention was to selectively disable the plugins specifically for the API requests coming to the server. How to do that on WordPress ? Before jumping to the solution, below are some backgrounds:

Basic flow of WordPress init for loading the plugins.

WordPress loads it’s core environment –> Must Use plugins are loaded –> if you’re on multisite setup – Network activated plugins are loaded –> Registers a few initial post types –> Sets up the theme directory –> finally loads the currently active plugins.

The relative lines of wp-settings.php:

<?php
// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin )
    include_once( $plugin );
unset( $plugin );

wp_get_active_and_valid_plugins() is the function which fetches all the active plugins and load them. This function gets these active plugins from the database where the list of active plugins are stored as an array in the wpdb_options table with option name as “active_plugins”. So, we can use the filter hook “option_active_plugins” to do modification to the list active plugins for temporary period.

Code for selectively disable plugins on WordPress for a specific request. This must be made a MU (Must Use) plugin to load and remove the plugins which are to be removed while initializing the WP.

<?php
/*
Plugin Name: API Load Time Enhancement
Plugin URI: https://subharanjan.com/ 
Description: Disables plugins for API requests in order to speed up the API response times. 
Version: 007
Author: Subh
Author URI: https://subharanjan.com/ 
*/
$listener_term = '/webservices/';
$current_url   = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '';

// listener for the thin load
if ( strstr( $current_url, $listener_term ) ) {
	add_filter( 'option_active_plugins', 'api_request_disable_plugin' );
}
function api_request_disable_plugin( $plugins ) {
	$plugins_not_needed = array(
		'backupwordpress/backupwordpress.php',
		'wordfence/wordfence.php',
		'contact-form-7-to-database-extension/contact-form-7-db.php',
		'contact-form-7/wp-contact-form-7.php',
		'wp-piwik/wp-piwik.php',
		'simple-responsive-slider/simple-responsive-slider.php',
		'google-sitemap-plugin/google-sitemap-plugin.php',
		'category-page-icons/menu-compouser.php',
		'easy-fancybox/easy-fancybox.php',
		'business-owner-switch/business-owner-switch.php',
		'wordpress-seo/wp-seo.php'
	);

	foreach ( $plugins_not_needed as $plugin ) {
		$key = array_search( $plugin, $plugins );
		if ( false !== $key ) {
			unset( $plugins[ $key ] );
		}
	}

	return $plugins;
}

10 thoughts on “Selectively disable plugins on WordPress for a specific request”

  1. Pingback: aggass2001 on "how to use this code to stop plugin working in certain pages" * Best Wordpress Themes - Reviews

      1. Hi,

        I tried your solution, but it was unsuccessful.
        Would it matter if this is on a subdomain ?

        Here is the code with your modification :

        <?php
        /*
        Plugin Name: API Load Time Enhancement
        Plugin URI: http://subharanjan.com/
        Description: Disables plugins for API requests in order to speed up the API response times.
        Version: 007
        Author: Subh
        Author URI: http://subharanjan.com/
        */
        $listener_term = 'http://subharanjan.com&#039;;

        // listener for the thin load
        if ( rtrim( $current_url, '/\\' ) == $listener_term ) {
        add_filter( 'option_active_plugins', 'api_request_disable_plugin' );
        }
        function api_request_disable_plugin( $plugins ) {
        $plugins_not_needed = array(
        'was-this-helpful-pro/was-this-helpful-pro.php',
        'backupwordpress/backupwordpress.php',
        'wordfence/wordfence.php',
        'contact-form-7-to-database-extension/contact-form-7-db.php',
        'contact-form-7/wp-contact-form-7.php',
        'wp-piwik/wp-piwik.php',
        'simple-responsive-slider/simple-responsive-slider.php',
        'google-sitemap-plugin/google-sitemap-plugin.php',
        'category-page-icons/menu-compouser.php',
        'easy-fancybox/easy-fancybox.php',
        'business-owner-switch/business-owner-switch.php',
        'wordpress-seo/wp-seo.php'
        );

        foreach ( $plugins_not_needed as $plugin ) {
        $key = array_search( $plugin, $plugins );
        if ( false !== $key ) {
        unset( $plugins[ $key ] );
        }
        }

        return $plugins;
        }
        '

          1. Hehe, I realized that .. And tried that. But no matter what I change or try , im unable to make this code work for the frontpage.
            Can you show me a working example ?
            Thanks

  2. Hello, looks great. I have many plugins and many different sites I would like to use this on. Is it possible to disable all plugins except for one or two that match? So, you are selectively disabling, I want to disable all and enable a couple selectively.

    Thanks!

  3. Hi Subharanjan,

    Thanks for the code:)

    But there is one problem in my case, I don’t want the $listener_term = ‘/webservices/’; to affect its children pages.

    For example I want to disable the plugin on /movies/ page but not /movies/hero etc..

    Any ideas, thanks.

Leave a Reply