WordPress at Your Fingertips

How do I disable some plugins in the 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

Kama_Hard_Disable_Plugins_On_Front::init();

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

	/**
	 * List of plugins you want to deactivate on front.
	 * Specify plugin`s folder name.
	 */
	public const DISABLE_PLUGINS = [
		//'meta-box',
		//'tiny-compress-images',
	];

	public static function init(){

		if( ! self::DISABLE_PLUGINS ){
			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.
	 */
	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::DISABLE_PLUGINS as $folder_name ){
			if( isset( $_plugins[ $folder_name ] ) ){
				unset( $active_plugins[ $_plugins[ $folder_name ] ] );
			}
		}

		return $active_plugins;
	}

}

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

--

Original question: https://wp-kama.ru/question/kak-otklyuchit-nekotorye-plaginy-vo-front-end

No comments
    Log In