-1

I am trying to remove the filter in JetPack's Related Posts module that adds the element to the DOM that the module's JavaScript populates with the related posts. Once I remove it, I can achieve my actual goal of being able to add it to various places besides the options the plugin provides.

The content filter I'm trying to remove looks like this:

   public function filter_add_target_to_dom( $content ) {
    if ( !$this->_found_shortcode ) {
        $content .= "\n" . $this->get_target_html();
    }

    return $content;
}

I've tried this, in a separate plugin:

add_action( 'plugins_loaded', 'slug_remove_jp_content_filter' , 2001 );
function slug_remove_jp_content_filter() {
   $jprp = Jetpack_RelatedPosts( get_current_blog_id() );
   remove_filter( 'the_content', array( $jprp, 'filter_add_target_to_dom' )    );
}

I have also tried this:

class myclass{

function __construct() {
    add_action( 'plugins_loaded', array( $this, 'remove_default_content_filter' ), 2001 );
}
function remove_default_content_filter() {
    $jprp = Jetpack_RelatedPosts( get_current_blog_id() );
    remove_filter( 'the_content', array( $jprp, 'filter_add_target_to_dom' ) );
}


}

new myclass();

In both cases I tried getting the JetPack Related Post class object with $jprp = Jetpack_RelatedPosts::init(); instead.

In the debug console I am able to get an object of the class with both $jprp = Jetpack_RelatedPosts::init(); and with $jprp = new Jetpack_RelatedPosts( get_current_blog_id() );

Not sure what else to try.

1
2
$jprp = Jetpack_RelatedPosts::init();
remove_filter( 'the_content', array( $jprp, 'filter_add_target_to_dom' ) );

Should work fine. The Jetpack_RelatedPosts init function implements the singleton pattern, so calling it will return the already instantiated instance, which you can use to remove the filter.

Just make sure you call your code late enough. The main module is hooking the add_action to functions coming from the 'wp' action hook, so you need to hook after that. The 'plugins_loaded' and 'init' hooks would be too early. You could hook to the 'wp' action yourself, with a priority of greater than 10 and that should do the trick.

Not the answer you're looking for? Browse other questions tagged or ask your own question.