How Disable Plugins on Front-end?

Question: There is an interesting plugin "Plugin Organizer", which allows you to selectively disable the necessary plugins on the site. ie plugin in the admin will continue to perform its functions (eg, display fields or anything else that is needed only in the admin), in front-end it will not be loaded.

Is there any way to get by with a couple lines of code in the functions.php file for this?

I monitored some plugins through the Query Monitor plugin, it gave out what events are triggered during page generation, and then disabled these events using remove_action().

But there are plugins that are not so easy to disable events and there are too many of them. So I am interested, maybe there is a ready-made code version.

Answer (solution):

This can be done by hooking on option_active_plugins. Here's the ready-made code to do it. I couldn't find any other hooks to do it...

<?php

/**
 * Disables the connection of the specified plugins at the front and ajax requests.
 *
 * @version 0.8
 * @author  Kama (wp-kama.ru)
 */
abstract class Kama_Hard_Disable_Plugins_On_Front {

	protected static array $disable_foldernames = [];

	/**
	 * Gets currently disabled plugins slugs (folder names) list.
	 *
	 * @return array
	 */
	public static function get_disabled(): array {
		return self::$disable_foldernames;
	}

	/**
	 *
	 * @param array $plugins_names  List of plugins you want to deactivate on front.
	 *                              Specify plugin`s folder name.
	 */
	public static function init( array $plugins_names ): void {

		/**
		 * Allows you to filter plugins slugs (folder names) that will be disabled.
		 *
		 * @param string[] $plugins_names Array of folder names.
		 */
		self::$disable_foldernames = apply_filters( 'hard_disable_plugins__filter_names', $plugins_names );

		if( ! self::get_disabled() ){
			return;
		}

		is_multisite()
			// for multisite
			? add_filter( 'site_option_active_sitewide_plugins', [ __CLASS__, 'disable' ] )
			// for regular instalation
			: add_filter( 'option_active_plugins', [ __CLASS__, 'disable' ] );
	}

	/**
	 * Deletes the specified plug-ins from the array.
	 *
	 * The function is used on the hook `option_active_plugins` or `site_option_active_sitewide_plugins`.
	 * The plugins are specified inside the function.
	 * You must specify an array with the names of the folders of the plugins,
	 * which you want to remove from the array of active plugins.
	 *
	 * @param array $active_plugins  Array of active plugins.
	 *
	 * @return array array of active plugins.
	 */
	public static function disable( array $active_plugins ): array{

		// skip
		if(
			// wp cli
			defined( 'WP_CLI' ) ||
			// front (but not ajax)
			( is_admin() && ! defined( 'DOING_AJAX' ) ) ||
			// ajax request from admin-panel
			( defined( 'DOING_AJAX' ) && strpos( $_SERVER['HTTP_REFERER'], '/wp-admin/' ) )
		){
			return $active_plugins;
		}

		// handy array
		$_plugins = $active_plugins;
		foreach( $_plugins as & $plugin ){
			[ $folder_name, $main_file ] = explode( '/', $plugin );
			$plugin = $folder_name;
		}
		unset( $plugin );
		$_plugins = array_flip( $_plugins );

		// deactivate
		foreach( self::get_disabled() as $folder_name ){
			if( isset( $_plugins[ $folder_name ] ) ){
				unset( $active_plugins[ $_plugins[ $folder_name ] ] );
			}
		}

		return $active_plugins;
	}

}

Usage

Now we need use the class, where we need to specify the plugins we want to disable:

Kama_Hard_Disable_Plugins_On_Front::init( [
	'advanced-custom-fields-pro',
	'wp-crontrol',
	'tiny-compress-images',
] );

Important: The code must be inserted before the plugins start plugging! This can only be done by creating an MU plugin, i.e. you do the following:

  1. In the wp-content folder, create, if it is not there, the folder mu-plugins.

  2. In the created mu-plugins folder create any php file, let it be disable-plugins-on-front.php and copy this code to that file.

  3. Done! The plugins specified in the code will not load on the front area.

--

P.S. I Looked inside the Plugin Organizer - not a bad plugin. But it has a lot of settings, creates tables in the database for some reason, and in general, it is tricky somehow...