0

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

2
  • What is the goal here. Do you expect this to improve performance? Jan 9, 2022 at 4:21
  • Yes definitely. I want to disable elementor on specific pages as well, because some pages do not use it but elementor has still 10 sql queries and a lot of Scripps on those pages. So yes, I'm sure it will greatly improve performance
    – tim
    Jan 9, 2022 at 5:15

1 Answer 1

0

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;
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.