简体   繁体   English

仅在Wordpress主页/首页上禁用特定插件

[英]Disable specific plugin on Wordpress frontpage/homepage only

Im trying to disable a plugin on a Wordpress frontpage, to make it a bit lighter. 我试图禁用一个WordPress的首页上的插件,使其更轻。 I only want it disabled when the front page is displayed. 我只希望在显示首页时禁用它。

I got this working code where I put in wp-content/mu-plugins/ It disables one plugin on all pages except a specified /subpage/ This is great, but it's not exactly what I am after. 我在wp-content / mu-plugins /中放了这个工作代码,它在除指定的/ subpage /之外的所有页面上禁用了一个插件。这很棒,但这并不是我想要的。

How can I change this code to disable a plugin only when viewing the front page ? 如何仅在查看首页时更改此代码以禁用插件?

Here is the code : 这是代码:

add_filter( 'option_active_plugins', 'lg_disable_plugin' );
function lg_disable_plugin($plugins){

    if(strpos($_SERVER['REQUEST_URI'], '/subpage/') === FALSE AND strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === FALSE) {

        $key = array_search( 'was-this-helpful-pro/was-this-helpful-pro.php' , $plugins );

        if ( false !== $key ) {
            unset( $plugins[$key] );
        }
    }

    return $plugins;
}

I also found another code which works for other pages.... But its the same story , I cannot figure out how to make it work for the frontpage, only works on subpages** 我还发现了另一个适用于其他页面的代码...。但是,同样的故事,我无法弄清楚如何使其适用于首页,仅适用于子页面**

$listener_term = '/subpage/';
$current_url   = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '';

// listener for the thin load
if ( strstr( $current_url, $listener_term ) ) {
 add_filter( 'option_active_plugins', 'api_request_disable_plugin' );
}
function api_request_disable_plugin( $plugins ) {
 $plugins_not_needed = array(
 'backupwordpress/backupwordpress.php',
 'wordfence/wordfence.php',
 'contact-form-7-to-database-extension/contact-form-7-db.php',
 'contact-form-7/wp-contact-form-7.php',
 'wp-piwik/wp-piwik.php',
 'simple-responsive-slider/simple-responsive-slider.php',
 'google-sitemap-plugin/google-sitemap-plugin.php',
 'category-page-icons/menu-compouser.php',
 'easy-fancybox/easy-fancybox.php',
 'business-owner-switch/business-owner-switch.php',
 'wordpress-seo/wp-seo.php'
 );

 foreach ( $plugins_not_needed as $plugin ) {
 $key = array_search( $plugin, $plugins );
 if ( false !== $key ) {
 unset( $plugins[ $key ] );
 }
 }

 return $plugins;
}

Any ideas? 有任何想法吗?

I guess I should mention that: 我想我应该提到:

  1. The first example will disable the plugin on all pages except for /subpage/ 第一个示例将在/ subpage /以外的所有页面上禁用该插件
  2. The second code will disable all the listed plugins on /subpage/ 第二个代码将禁用/ subpage /上所有列出的插件

Neither of them work for frontpage/homepage, no matter what I try. 无论我尝试什么,它们都不适合首页/主页。

I didn't realise you ONLY wanted it to happen on the Front Page - the code snippet suggests you want it on some other pages too. 我没意识到您希望它在首页上发生-代码段表明您也希望它在其他页面上进行。

I was previously suggesting you could use is_home or is_front_page, but at the stage we are doing this, they have not been loaded yet - so an alternative here is to load in the REQUEST_URI and see if it is blank. 我之前曾建议您可以使用is_home或is_front_page,但在此阶段,我们尚未加载它们-因此,这里的另一种方法是加载REQUEST_URI并查看其是否为空白。 If it is blank, we will assume we are on the homepage - to cater for installations that may not be at the root, we compare the URL to the site's relative homepage location - for added safety, we encapsulate it within htmlspecialcharacters (probably not necessary). 如果为空白,则假定我们在主页上-为了迎合可能不是根目录的安装,我们将URL与站点的相对主页位置进行了比较-为了增加安全性,我们将其封装在htmlspecialcharacters中(可能没有必要) )。

Perhaps try something like this (look, it may not be the best way to do it, but it certainly works for me): 也许尝试这样的事情(看,这可能不是最好的方法,但对我当然有效):

add_filter( 'option_active_plugins', 'lg_disable_plugin' );
function lg_disable_plugin($plugins){
    if (htmlspecialchars(trim(wp_make_link_relative(get_site_url()),'/')) == htmlspecialchars(trim($_SERVER['REQUEST_URI'],'/'))) {
        $key = array_search( 'was-this-helpful-pro/was-this-helpful-pro.php' , $plugins );
        if ( false !== $key ) {
            unset( $plugins[$key] );
        }
    }

    return $plugins;
}

If you wanted to do this for multiple plugins, you could change the code to the following: 如果要对多个插件执行此操作,则可以将代码更改为以下内容:

    add_filter( 'option_active_plugins', 'lg_disable_plugin' );
    function lg_disable_plugin($plugins){
        if (htmlspecialchars(trim(wp_make_link_relative(get_site_url()),'/')) == htmlspecialchars(trim($_SERVER['REQUEST_URI'],'/'))) {
            $plugins_not_needed = array ('was-this-helpful-pro/was-this-helpful-pro.php',
           'pluginfolder/plugin-name.php');
            foreach ( $plugins_not_needed as $plugin ) {
                $key = array_search( $plugin, $plugins );
                if ( false !== $key ) {
                    unset( $plugins[ $key ] );
                }
            }
        }

        return $plugins;
    }

you could write an mu-plugin, if you don't have it, create a folder called "mu-plugins" inside the "wp-content" folder, then create a PHP file where you can write and try this code: 您可以编写一个mu插件,如果没有,请在“ wp-content”文件夹中创建一个名为“ mu-plugins”的文件夹,然后创建一个PHP文件,您可以在其中编写并尝试以下代码:

if( !is_admin() && empty( $_POST ) ){
    $uri = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    $uriArr = explode( '?',$uri );
    $uri = $uriArr[0];
    $home_uri = str_replace( 'https://','',str_replace( 'http://','',home_url( '/' ) ) );
    if ( $uri === $home_uri ) {
        $paths = array(
            'plugin_folder1/plugin1.php'
            'plugin_folder2/plugin2.php'
        );
        global $paths;
        add_filter( 'option_active_plugins', 'my_option_active_plugins' );
    }
}

function my_option_active_plugins( $plugins ){
    global $paths;
    foreach( $paths as $path ){
        $k = array_search( $path, $plugins );
        if( false !== $k ){
            unset( $plugins[$k] );
        }
    }

    return $plugins;
}

Just replace "plugin_folder1/plugin1.php" ... with the plugins you want to deactivate 只需将“ plugin_folder1 / plugin1.php”替换为您要停用的插件

Another option is to install the free plugin Freesoul Deactivate Plugins and you will be able to easily deactivate the plugins you want for each page, post, custom post, and archive. 另一个选择是安装免费插件Freesoul停用插件,您将能够轻松地停用每个页面,帖子,自定义帖子和存档所需的插件。

Here is an abstraction I wrote that allows you to specify a route and pass it a list of plugins. 这是我写的一个抽象,允许您指定路由并将其传递给插件列表。 Print out the plugins to a log or the screen to get their names then just add them to the array and they will get filtered. 将插件打印到日志或屏幕上以获取它们的名称,然后将它们添加到阵列中即可对其进行过滤。

Another approach to consider would be replacing targets with a whitelist, so you have to define less things per route (if you have a lot of plugins going). 要考虑的另一种方法是用白名单替换目标,因此您必须为每个路由定义更少的内容(如果要使用的插件很多)。

/**
 * strpos() returns the start position of the substring if found, which 
 * if 0 will trigger false, so prepend something (in this case 'x') 
 * to offset it to return 1 (true).
 */
add_filter( 'option_active_plugins', function( $plugins ){

    $needle = '/some-route/path';
    $haystack = 'x' . $_SERVER['REQUEST_URI']; // prepend something to string 
    if (strpos($haystack, $needle) !== false) {
        $targets = [
            'wecreativez-woo-cart-share-and-save/wecreativez-woo-cart-share-and-save.php',
            'what-the-file/what-the-file.php',
            'woo-address-book/woocommerce-address-book.php',
            'ubermenu/ubermenu.php',
            'flexible-checkout-fields-pro/flexible-checkout-fields-pro.php',
            'ewww-image-optimizer/ewww-image-optimizer.php',
            'svg-support/svg-support.php',
            'gravityforms/gravityforms.php',
            'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
            'js_composer/js_composer.php',
        ];

        echo "Before Filter: Active Plugins for ($needle):";
        echo '<pre>';
        print_r($plugins);

        foreach( $targets as $target ) {
            array_splice($plugins, array_search($target, $plugins), 1);
        }

        echo "After Filter: Active Plugins for ($needle):";
        print_r($plugins);
        die;

    }

    return $plugins;

}, PHP_INT_MAX);

This could be expanded to use an array of needles (routes) that have an array of plugins to remove for even more control. 可以将其扩展为使用针阵列(路径),这些针阵列具有一系列要删除的插件,以进行更多控制。

Something like: 就像是:

$needles = [
    '/route-1/' => [
        'gravityforms/gravityforms.php',
    ],
    '/shop/' => [
        'gravityforms/gravityforms.php',
        'ewww-image-optimizer/ewww-image-optimizer.php',
        'flexible-checkout-fields-pro/flexible-checkout-fields-pro.php',
    ],
    'search' [
        'ewww-image-optimizer/ewww-image-optimizer.php',
        'flexible-checkout-fields-pro/flexible-checkout-fields-pro.php',
    ],
];



$haystack = 'x' . $_SERVER['REQUEST_URI']; // prepend something to string 

foreach ($needles as $needle => $targets) {

    if (strpos($haystack, $needle) !== false) {

         foreach( $targets as $target ) {
               array_splice($plugins, array_search($target, $plugins), 1);
         }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM