How to Disable a Plugin On the Fly in WordPress

Have you ever encountered conflicts or errors caused by a plugin that you don’t need on a specific page (whether it’s the front-end or the dashboard)?

If yes, an easy workaround is to disable on the fly this plugin!

What does it mean “disable on the fly”?

Disabling a plugin on the fly means making it inactive only for the page load of a specific page that meets certain criteria, while keeping it active on all other pages of the website.

How Can I Do This?

You need to create a must-use plugin in your WordPress installation and add your logic there to determine on which pages the plugin should be inactive.

What is a Must-Use Plugin?

A must-use plugin (MU plugin) is a special type of plugin in WordPress that is automatically activated and cannot be deactivated from the admin dashboard. These plugins are typically used for essential site functionality, as they are located in a specific directory (wp-content/mu-plugins) and are loaded before regular plugins. MU plugins are ideal for implementing site-wide logic or functionality that needs to be always active, such as custom hooks or settings.

Here, you can find a tutorial on how to add a hook to your site via a must-use plugin, among other methods that are suggested.

Let’s Write the Code for This File

<?php
/**
 * Plugin Name: On the fly plugin deactivation
 * Description: This mu plugin disables on the fly a plugin.
 * Version: 1
 */

if( !class_exists('Pexlechris_Disable_On_The_Fly_Plugin') ) {
	class Pexlechris_Disable_On_The_Fly_Plugin
	{

		/**
		 * The constructor file of the plugin contains metadata and information about the plugin.
		 * This information is usually placed in comments at the beginning of the file.
		 */
		private $plugin_constructor_file;

		public function __construct($plugin_constructor_file)
		{
			$this->plugin_constructor_file = $plugin_constructor_file;
			add_filter('option_active_plugins', [$this, 'hook_fallback']);
		}

		public function hook_fallback($active_plugins)
		{

			if ($this->meets_criteria()) {

				$key = array_search($this->plugin_constructor_file, $active_plugins);
				if ($key !== false) {
					unset($active_plugins[$key]);
				}

			}

			return $active_plugins;

		}

		private function meets_criteria()
		{
			/**
			 * In the backend, the global $pagenow variable represents the PHP file currently loaded in your browser.
			 *
			 * For example, when you're on https://pexlechris.dev/wp-admin/plugins.php,
			 * $pagenow will be equal to 'plugins.php'.
			 *
			 * In the front-end, $pagenow is typically equal to 'index.php'.
			 */
			global $pagenow;

			/**
			 * is_admin function determines whether the current request is for an administrative interface page.
			 */
			$is_admin = is_admin();


			// In plugins.php, we want to ensure the real status of the plugin.
			// It's recommended to keep this if-condition to check the plugin status accurately.
			if ($is_admin && $pagenow == 'plugins.php') {
				return false;
			}


			$protocol = is_ssl() ? 'https' : 'http';
			$domain = $_SERVER['HTTP_HOST'];
			$request_uri_with_all_get_parameters = $_SERVER['REQUEST_URI'];
			$request_uri_without_get_parameters = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

			$full_url = $protocol . '://' . $domain . $request_uri_with_all_get_parameters;
			$full_url_without_get_parameters = $protocol . '://' . $domain . $request_uri_without_get_parameters;


			// if we want to disable it for this page https://mysite.gr/page/ we can do one of these
			if ($full_url == 'https://mysite.gr/page/') {
				return true;
			}
			// OR
			if ($full_url == home_url() . '/page/') { // good for WP installations in a subdirectory
				return true;
			}
			// OR
			if ($request_uri_without_get_parameters == '/page/') { // NOT GOOD for WP installations in a subdirectory
				return true;
			}


			// if you want to disable it, only for the Dashboard page post.php page
			if ($is_admin && $pagenow == 'post.php') {
				return true;
			}

			// if you want to disable it, for not only one Dashboard page
			if ($is_admin && in_array($pagenow, ['post.php', 'post-new.php'])) {
				return true;
			}

			return false;
		}

	}

}


new Pexlechris_Disable_On_The_Fly_Plugin( 'plugin_folder/plugin_file.php' );
// If you want to disable another plugin based on the same criteria,
// just disable the comment // from above line
// new Pexlechris_Disable_On_The_Fly_Plugin( 'plugin_folder2/plugin_file2.php' );//

“What Changes Do You Need to Make to This Code?”

  1. Change the plugin constructor file at the bottom of this code
    You can find this information using your browser’s inspector on the Plugins page in the Dashboard, specifically from the data-plugin attribute of the <tr> HTML element for this plugin.
  1. Write your own logic in the meets_criteria method to determine when to return true.
  2. Upload the must-use plugin to your WordPress installation via FTP/SFTP.

By following these steps, you can easily disable a plugin on the fly, improving your site’s performance and avoiding unnecessary conflicts. This method provides flexibility, allowing you to target specific pages without affecting the rest of your site.