Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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