To conditionally deactivate a plugin only for specific requests, you should avoid modifying the option_active_plugins directly. Instead, you can prevent the plugin’s code from running by either:
- Using hooks to remove specific actions and filters that the plugin adds.
- Overriding the plugin’s main file from being included based on the request conditions.
Here’s how you can achieve this by overriding the plugin’s main file inclusion:
add_action('plugins_loaded', function () {
// Check if we are on the specified backend URL
if (
strpos($_SERVER['REQUEST_URI'], '/wp-admin/') !== false
&& isset($_GET['action']) && $_GET['action'] == 'elementor'
&& isset($_GET['post'])
&& $_SERVER['REQUEST_METHOD'] == 'GET'
) {
// List of plugins to disable
$denylist_plugins = [
'wp-data-access/wp-data-access.php',
];
foreach ($denylist_plugins as $plugin) {
// Check if the plugin is active
if (is_plugin_active($plugin)) {
// Override the plugin main file inclusion
add_action('pre_current_active_plugins', function () use ($plugin) {
if (isset($GLOBALS['wp_plugin_paths'][WP_PLUGIN_DIR . '/' . $plugin])) {
unset($GLOBALS['wp_plugin_paths'][WP_PLUGIN_DIR . '/' . $plugin]);
}
}, 1);
}
}
}
}, 1);
@harpalsinhchauhan
Tested your code, it didn’t manage to deactive the listed plugins.
Also I think plugins_loaded triggers after main plugin php files are loaded. See https://developer.ww.wp.xz.cn/reference/hooks/plugins_loaded/
Hello @wpshushu ,
Can you try with the updated below code :
add_filter('pre_option_active_plugins', function ($plugins) {
// Check if the URL matches the desired pattern
if (
strpos($_SERVER['REQUEST_URI'], '/wp-admin/post.php') !== false
&& (isset($_GET['action']) && $_GET['action'] == 'elementor')
&& isset($_GET['post'])
&& $_SERVER['REQUEST_METHOD'] == 'GET'
) {
// Define the plugins you want to deactivate
$denylist_plugins = [
'wp-data-access/wp-data-access.php',
];
// Remove the plugins from the active plugins list for this request only
foreach ($plugins as $key => $plugin) {
if (in_array($plugin, $denylist_plugins)) {
unset($plugins[$key]);
}
}
}
return $plugins;
}, 10, 1);