"Disable" specific plugins on certain pages

I want to disable specific plugins on specific frontpages, but not disable them in the backend, i.e. really disable them in the DB. Just NOT load them, as if they weren't installed.

Did this via mu-plugin very early in the code:

function strposa($haystack, $needles=array(), $offset=0) { // Like strpos for an Array of needles
    foreach($needles as $needle) {
        if(strpos($haystack, $needle, $offset) !== false)
            return true;
    }
    return false;
}

add_filter('option_active_plugins', function ($plugins)
{    
    if(!wp_doing_ajax()  !wp_doing_cron()  !is_admin()) 
    {
        $remove_plugins_frontpage = array('duplicator-pro', 
                                          'block-specific-plugin-updates', 
                                          'delete-expired-transients');
        foreach($plugins as $key = $plug) {
            if(strposa($plug, $remove_plugins_frontpage))
                unset($plugins[ $key ]);        
        }
    }       

    return $plugins;
});

I.e. do as if for example Duplicator wouldnt be installed, because I dont need all the frontend actions in my frontend. I just need Duplicator in the backend AND during Cron calls.

Unfortunately, this disables the plugins mentioned above also in the DB. That means: When I go to the backend, those plugins are disabled which is quite bad.

Any ideas how to circle around this problem?

Thanks so much

Topic mu-plugins plugins Wordpress performance

Category Web


I found the issue with the code:

The code is good and basically works.

The only thing is: There are specific plugins, if installed, which request the option of activated plugins and WRITE it back to the database to "sort" the order of activation. E.g. WPML, which needs/wants to be activated as soon as possible. So if you'd use my code from above, you basically deactivate specific plugins in the DB completely if you use WPML or similarly.

That's why one addition has to be made: Checking to apply the filter only if the function has been called by wp_get_active_and_valid_plugins():

function strposa($haystack, $needles=array(), $offset=0) { // Like strpos for an Array of needles
    foreach($needles as $needle) {
        if(strpos($haystack, $needle, $offset) !== false)
            return true;
    }
    return false;
}

add_filter('option_active_plugins', function ($plugins)
{    
    $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 7); # Limit 7
    $called_by = false;
    foreach($trace as $entry) {
        if(@$entry['function'] == 'wp_get_active_and_valid_plugins') {
            $called_by = true;
            break;
        }
    }
    if(!$called_by) 
        return $plugins; # Only modify the option when called by wp_get_active_and_valid_plugins()
        
    if(!wp_doing_ajax() && !wp_doing_cron() && !is_admin()) 
    {
        $remove_plugins_frontpage = array('duplicator-pro', 
                                          'block-specific-plugin-updates', 
                                          'delete-expired-transients');
        foreach($plugins as $key => $plug) {
            if(strposa($plug, $remove_plugins_frontpage))
                unset($plugins[ $key ]);        
        }
    }       

    return $plugins;
});

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.