• http://subharanjan.com/selectively-disable-plugins-on-wordpress-for-a-specific-request/

    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: 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 = '/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;
    }
  • The topic ‘how to use this code to stop plugin working in certain pages’ is closed to new replies.