“Disable” specific plugins on certain pages

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