最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

performance - "Disable" specific plugins on certain pages

programmeradmin3浏览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

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

Share Improve this question edited Jan 8, 2022 at 21:07 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jan 8, 2022 at 20:42 timtim 1648 bronze badges 2
  • What is the goal here. Do you expect this to improve performance? – Jacob Peattie Commented 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 Commented Jan 9, 2022 at 5:15
Add a comment  | 

1 Answer 1

Reset to default 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;
});
发布评论

评论列表(0)

  1. 暂无评论