【问题标题】:How to disable specific plugin on Wordpress backend / edit product page如何在 Wordpress 后端/编辑产品页面上禁用特定插件
【发布时间】:2019-08-19 14:05:45
【问题描述】:

我试图找到一种解决方案来禁用 Wordpress 管理区域上的特定插件。问题是,对于构建 WooCommerce 商店,我使用 Divi Builder,当您尝试对其进行编辑时,有时在产品页面上可能会使用 50mb 的资源......如果我在那里禁用一些插件,那么加载时间会快得多。我在其他主题上找到了以下代码:

add_filter( 'option_active_plugins', 'lg_disable_cart66_plugin' );

function lg_disable_cart66_plugin($plugins){

      if(strpos($_SERVER['REQUEST_URI'], '/store/') === FALSE AND strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === FALSE) {
         $key = array_search( 'cart66/cart66.php' , $plugins );
         if ( false !== $key ) unset( $plugins[$key] );
      }

      return $plugins;
 }

但不知道如何修改它,所以它只会在后端禁用所选插件。换句话说:我不希望在我编辑 WooCommerce 产品页面时加载插件。

我们将不胜感激。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    由于在加载任何插件之前触发了“option_active_plugins”,我们需要将代码放到mu-plugins 目录中。还请记住,这些插件是在主查询初始化之前运行的——这就是为什么我们无法访问许多功能,尤其是总是返回 false 的条件标签。

    请粘贴以下代码或将gist 下载到您的 wp-content 文件夹中的 mu-plugins 文件夹中。它只会在帖子和页面区域禁用插件。

    <?php
    /*
    Plugin Name: Disable Plugin for URl
    Plugin URI: https://www.glowlogix.com
    Description: Disable plugins for for specific backend pages.
    Author: Muhammad Usama M.
    Version: 1.0.0
    */
    add_filter( 'option_active_plugins', 'disable_plugins_per_page' );
    function disable_plugins_per_page( $plugin_list ) {
    
        // Quit immediately if not post edit area.
        global $pagenow;
        if (( $pagenow == 'post.php' || $pagenow == 'edit.php' )) {
            $disable_plugins = array (
                // Plugin Name
                'hello.php',
                'developer/developer.php'
            );
            $plugins_to_disable = array();
            foreach ( $plugin_list as $plugin ) {
                if ( true == in_array( $plugin, $disable_plugins ) ) {
                    //error_log( "Found $plugin in list of active plugins." );
                    $plugins_to_disable[] = $plugin;
                }
            }
            // If there are plugins to disable then remove them from the list,
            // otherwise return the original list.
            if ( count( $plugins_to_disable ) ) {
                $new_list = array_diff( $plugin_list, $plugins_to_disable );
                return $new_list;
            }   
        }
        return $plugin_list;
    }
    

    您可以将 $disable_plugins 替换为需要禁用的插件列表。

    【讨论】:

    • 好的,我刚刚使用 WordPress 进行了测试,并且运行良好。如果可能,请在它崩溃后分享 error_log 消息。
    • 知道怎么做吗?当我使用它时,我无法再访问我的仪表板了
    • 我已经改进了解决方案并创建了 mu-plugin 以满足您的要求。
    • 非常感谢!虽然它仍然不能正常工作:-S 例如,我试图在编辑页面上禁用查询监视器插件。它从顶部菜单栏中消失了,但它仍然加载了它的 JS 和 CSS,这会减慢编辑页面的速度:take.ms/tM6TF 我在这里使用 Divi Builder,当所有插件都处于活动状态时,在编辑页面上它可以使用 47MB资源(在 Chrome 开发网络选项卡中:take.ms/EWVvk)。这太疯狂了,所以我必须以某种方式限制它:(当我从插件页面手动禁用插件时,他们不会加载他们的 CSS 和 JS ..
    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多