【问题标题】:Removing a filter added by a Class from a plugin从插件中删除由类添加的过滤器
【发布时间】:2013-11-08 14:11:42
【问题描述】:

我有一个在所有帖子和页面上运行的插件,我想从我为谢谢页面创建的自定义模板的内容中删除它。

我打印出所有添加到内容中的过滤器,我想参考的一个如下

[18de263ad73c07944f622a52d10d6e0ewpsocialite_filter_content] => Array
            (
                [function] => Array
                    (
                        [0] => wpsocialite Object
                            (
                                [options:private] => 
                            )

                        [1] => wpsocialite_filter_content
                    )

                [accepted_args] => 1
            )

    )

我尝试了许多不同的公司,但都无法做到。我希望我的 functions.php 中的最终代码看起来像这样

<?php if(is_page_template(thank-you.php)){
   remove_filter('the_content', 'wpsocialite_filter_content');
}

主要问题是过滤器是由类对象添加的。这是添加它的代码

if (!class_exists("wpsocialite")) {

class wpsocialite {
    public static $instance;
    private $options;

    public function WPSocialite() {
        $this->__construct();
    }

    function __construct() {
        self::$instance = $this;

        add_action(     'init',                     array( $this, 'init'                            ) );
        add_action(     'wp_footer',                array( $this, 'wpsocialite_localize_script'     ), 20);

        add_action(     'admin_init',               array( $this, 'admin_init'                      ) );
        add_action(     'admin_footer',             array( $this, 'admin_footer'                    ), 20);

        add_filter(     'body_class',               array( $this, 'wpsocialite_body_class'          ) );
        add_filter(     'the_content',              array( $this, 'wpsocialite_filter_content'      ) );
        add_filter(     'mce_external_plugins',     array( $this, 'wpsocialite_shortcode_plugin'    ) );
        add_filter(     'mce_buttons',              array( $this, 'wpsocialite_shortcode_button'    ) );
        add_filter(     'plugin_action_links',      array( $this, 'wpsocialite_settings_link'       ), 10, 2 );
        add_shortcode(  'wpsocialite',              array( $this, 'wpsocialite_shortcode'           ) );

        if( get_option( 'wpsocialite_excerpt' ) == 1 ){
            add_filter( 'the_excerpt',              array( $this, 'wpsocialite_filter_content'      ) );
        }

    } // __construct

如何引用它来删除它?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    因为我是通过 Google 找到的,但没有一个答案有效:

    function remove_silly_object_filter($filter_name, $class_name, $function_name){
      global $wp_filter;
      foreach($wp_filter[ $filter_name ]->callbacks as $priority => $pri_data){
        foreach ($pri_data as $cb => $cb_data){
          if (is_array($cb_data['function']) && get_class($cb_data['function'][0]) == $class_name && $cb_data['function'][1] == $function_name){
            $object = $cb_data['function'][0];
            $priority_to_remove = $priority;
            $function = $cb_data['function'][1];
            break;
          }
        }
        if (isset($object)) break;
      }
      remove_filter($filter_name,  array($object, $function), $priority_to_remove);
    }
    

    【讨论】:

      【解决方案2】:

      创建一个mini plugin 并使用like:

      <?php
      /* Plugin Name: Remove other plugin filter */
      
      add_action( 'plugins_loaded', function() 
      {
          add_action( 'template_redirect', function() 
          {
              if( is_page_template( 'thank-you.php' ) )
                  remove_filter( 'the_content', array( wpsocialite::$instance, 'wpsocialite_filter_content' ) );
          });
      });
      

      我使用下面的类作为另一个插件的模拟:

      <?php
      /* Plugin Name: Original plugin */
      
      class wpsocialite {
          public static $instance;
          private $options;
          public function WPSocialite() {
              $this->__construct();
          }
          function __construct() {
              self::$instance = $this;
              add_filter( 'the_content', array( $this, 'wpsocialite_filter_content' ) );
          } 
          function wpsocialite_filter_content( $content ) {
              return 'no content';
          }
      }
      new wpsocialite();
      

      参考:remove_action or remove_filter with external classes?

      【讨论】:

      • 我之前尝试过,我编辑了这个问题,希望能解释我的头痛是从哪里来的。
      • 感谢您尝试 b。它把 wordpress 的空白页扔给我。
      • De-anonymize PHP5.3+ 函数,你必须运行 5.2 或更低版本。
      【解决方案3】:

      你可以试试这样的东西,它对我有用:

      if ( class_exists( 'YITH_WC_Save_For_Later_Premium' ) ) {
          $yith_wc_save_for_later_premium = YITH_WC_Save_For_Later_Premium::get_instance(); /* <-- this is the key */
          remove_filter( 'woocommerce_cart_item_name', array( $yith_wc_save_for_later_premium, 'print_add_link_in_list'), 20 );
      }
      

      【讨论】:

        【解决方案4】:

        我知道这是一个老问题,但我在 Google 上遇到过,所以以防其他人也这样做......

        我相信静态的$instance 变量(分配给$self)是关键,所以它应该很简单:

        <?php if(is_page_template(thank-you.php)){
           remove_filter('the_content', array( wpsocialite::$instance, 'wpsocialite_filter_content' ));
        }
        

        【讨论】:

          猜你喜欢
          • 2016-08-23
          • 1970-01-01
          • 2017-10-19
          • 1970-01-01
          • 2012-08-14
          • 1970-01-01
          • 2023-04-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多