• Hi,

    I was trying to deactivate a plugin programmatically for backend URLs like: /wp-admin/post.php?post=123&action=elementor using the option_active_plugins HOOK. I placed the code under /wp-content/mu-plugins/ and tested it, only to find out that the plugin gets deactivated permanently. What am I missing?

    add_filter( ‘option_active_plugins’, function ($plugins) {

    if (
    strpos($_SERVER[‘REQUEST_URI’], ‘/wp-admin/’) !== false
    && (isset($_GET[‘action’]) && $_GET[‘action’] == ‘elementor’)
    && isset($_GET[‘post’])
    && $_SERVER[‘REQUEST_METHOD’] == ‘GET’
    ) {

    $denylist_plugins = [
    “wp-data-access/wp-data-access.php”,
    ];

    foreach ( $plugins as $key => $plugin ) {
    if ( in_array( $plugin, $denylist_plugins ) ) {
    unset($plugins[$key]);
    continue;
    }
    }
    }

    return $plugins;

    }, 888, 1);

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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:

    1. Using hooks to remove specific actions and filters that the plugin adds.
    2. 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);
    Thread Starter wpshushu

    (@wpshushu)

    @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.www.ads-software.com/reference/hooks/plugins_loaded/

    Harpalsinh Chauhan

    (@harpalsinhchauhan)

    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);
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.