从插件中删除由类添加的过滤器

时间:2013-10-29 12:13:49

标签: php wordpress

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

我打印出添加到内容中的所有过滤器,我要引用的过滤器如下

[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');
}

主要问题是过滤器是由Class Object添加的。这是添加它的代码

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

如何引用它以将其删除?

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

创建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?

答案 2 :(得分:0)

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

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