Skip to content

Instantly share code, notes, and snippets.

@juliquiron
Created November 22, 2019 13:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juliquiron/bb39a62b07228f63393926944d8f3cd3 to your computer and use it in GitHub Desktop.
Save juliquiron/bb39a62b07228f63393926944d8f3cd3 to your computer and use it in GitHub Desktop.
Disable plugins in specific URLs in Wordpress
<?php
/**
* Plugin disabler for specific URLs
*
* @package Timeular
* @author Julià Mestieri
* @version 0.1.0
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License version 2 (GPLv2)
*/
namespace YourNamespace;
/**
* Class Plugins_Disabler.
*/
class Plugins_Disabler {
/**
* Maps the plugins to disable in a given URL regex match.
*
* @var array
*/
private static $url_plugin_map = [
'#^/checkout#' => [ 'js_composer/js_composer.php' ],
'#^/cart#' => [ 'js_composer/js_composer.php' ],
];
/**
* Initialize the plugin behavior.
*
* @param array $plugins Plugins enabled in the site.
*/
public static function init( $plugins ) {
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
return $plugins;
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
$check_uri = rtrim( strtolower( sanitize_text_field( $_SERVER['REQUEST_URI'] ) ), '/\\' );
foreach ( self::$url_plugin_map as $regex => $disable_plugins ) {
if ( preg_match( $regex, $check_uri ) ) {
foreach ( $disable_plugins as $plugin ) {
$key = array_search( $plugin, $plugins, true );
if ( false !== $key ) {
unset( $plugins[ $key ] );
}
}
}
}
return $plugins;
}
}
add_filter( 'option_active_plugins', [ '\YourNamespace\Plugins_Disabler', 'init' ], 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment