Created
November 22, 2019 13:15
-
-
Save juliquiron/bb39a62b07228f63393926944d8f3cd3 to your computer and use it in GitHub Desktop.
Disable plugins in specific URLs in Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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