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

问题描述 投票:2回答:3

我试图在Wordpress首页上禁用插件,使其更轻一些。我只想在显示首页时禁用它。

我得到了这个工作代码,我放入了wp-content / mu-plugins /它在所有页面上禁用了一个插件,除了指定的/子页面/这很好,但它并不完全是我所追求的。

如何在查看首页时更改此代码以禁用插件?

这是代码:

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

我还发现了另一个适用于其他页面的代码....但是同样的故事,我无法弄清楚如何使它适用于首页,只适用于子页面**

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

有任何想法吗?

我想我应该提到:

  1. 第一个示例将禁用除/ subpage /之外的所有页面上的插件
  2. 第二个代码将禁用/ subpage /上列出的所有插件

无论我尝试什么,它们都不适用于首页/主页。

php wordpress optimization wordpress-plugin
3个回答
4
投票

我没有意识到你只想在头版上发生它 - 代码片段表明你也希望它在其他一些页面上。

我之前建议您可以使用is_home或is_front_page,但是在我们这样做的阶段,它们尚未加载 - 所以这里的替代方法是加载REQUEST_URI并查看它是否为空白。如果它是空白的,我们将假设我们在主页上 - 为了满足可能不在根目录的安装,我们将URL与网站的相对主页位置进行比较 - 为了增加安全性,我们将其封装在htmlspecialcharacters中(可能没有必要) )。

也许尝试这样的东西(看,它可能不是最好的方法,但它肯定适合我):

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

如果您想为多个插件执行此操作,可以将代码更改为以下内容:

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

0
投票

你可以写一个mu-plugin,如果你没有它,在“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;
}

只需将“plugin_folder1 / plugin1.php”替换为您要停用的插件

另一种选择是安装免费插件Freesoul Deactivate插件,您将能够轻松地为每个页面,帖子,自定义帖子和存档停用所需的插件。


0
投票

这是我写的一个抽象,允许你指定一个路由并传递一个插件列表。将插件打印到日志或屏幕以获取其名称,然后将它们添加到阵列中,它们将被过滤。

另一种考虑的方法是用白名单替换目标,因此你必须为每条路线定义更少的东西(如果你有很多插件)。

/**
 * 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);

这可以扩展为使用具有一系列插件的针(路线)阵列来移除以进行更多控制。

就像是:

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

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.