0

I want to replace a template from a third-party plugin included by a class function with a customized one. I am trying to override that class function by extending the parent class, but for now this just is doubling the expected output. How to remove the original function? I need to remove the filter that is added in the parent class? How to do that?

The main class looks like this:

class Import_Some_Events_Cpt {

    public function __construct() {
        add_filter( 'the_content', array( $this, 'some_events_meta_before_content' ) ); 
    }

    function some_events_meta_before_content( $content ) { 
        $event_details = $this->some_events_get_event_meta( get_the_ID() );
        $content = $event_details . $content;
        return $content;
    }

    function some_events_get_event_meta() {
        //here is included the original plugin template
    }
}

My extending class is looking like this:

class Import_Some_Events_Cpt_Ext extends Import_Some_Events_Cpt {

    function some_events_meta_before_content( $content ) { 
        $event_details = $this->some_events_get_event_meta( get_the_ID() );
        $content = $event_details . $content;
        return $content;
    }

    function some_events_get_event_meta() {
        //here is included the customized template
    }
}

UPDATE

The original class Import_Some_Events_Cpt is instanciated in an other class:

class Import_Some_Events{
    private static $instance;
    public static function instance() {
        if( ! isset( self::$instance ) && ! (self::$instance instanceof Import_Some_Events ) ) {
            self::$instance = new Import_Some_Events;
            self::$instance->setup_constants();

            add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ) );

            self::$instance->cpt    = new Import_Some_Events_Cpt();

        }
        return self::$instance; 
    }
}

This class is instanciated from a separate function:

function run_import_some_events() {
    return Import_Some_Events::instance();
}

And the above function is set to a variable:

$ife_events = run_import_some_events();

The all code is in an other file than the class I want to override.

Sorry if I make mistakes, I am not a programmer.

  • how and where in the code is instanciated the original class Import_Some_Events_Cpt ? – mmm Oct 14 '17 at 23:40
  • @mmm I updated the question. – Iurie Malai Oct 15 '17 at 5:50
1

Try something like the code below:

function wpse282960_remove_filter() {
    $run_import_some_events = run_import_some_events();
    remove_filter( 'the_content', array($run_import_some_events->cpt, 'some_events_meta_before_content') );
}
add_action( 'template_redirect', 'wpse282960_remove_filter' );
  • I solved my problem by modifying a little your solution (see the edit). – Iurie Malai Oct 15 '17 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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