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.
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: 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; }
Leave a Reply