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:

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.


    • May be you can directly match the home url ( hard coded ) with the current url and if it matches, then disable plugins not used/needed. Something like this…

      Thanks !!

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

        Plugin Name: API Load Time Enhancement
        Plugin URI:
        Description: Disables plugins for API requests in order to speed up the API response times.
        Version: 007
        Author: Subh
        Author URI:
        $listener_term = ';;

        // 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(

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

        return $plugins;

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


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