1

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

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

[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

如何引用它来删除它?

4

4 回答 4

3

因为我通过谷歌找到了这个,但没有一个答案有效:

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);
}
于 2017-05-12T16:25:47.027 回答
0

创建一个迷你插件并使用:

<?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 或 remove_filter?

于 2013-10-29T12:38:29.687 回答
0

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

我相信静态$instance变量(被赋值$self)是关键,所以它应该很简单:

<?php if(is_page_template(thank-you.php)){
   remove_filter('the_content', array( wpsocialite::$instance, 'wpsocialite_filter_content' ));
}
于 2021-12-20T15:14:13.080 回答
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 );
}
于 2016-12-16T22:28:15.647 回答