2016-04-15 4 views
1

Ich versuche, eine 3rd-Party-Plugin-Klasse innerhalb meiner Child Theme zu erweitern.WordPress - Wie 3rd Party Plugin-Klasse zu erweitern

Ich schließe meine neue Klasse des Init-Haken verwenden, der gut arbeitet:

//add_action('plugins_loaded', function(){ //never fires 
add_action('init', function(){ 

    require_once get_stylesheet_directory() . '/includes/my-comment-form.php'; 

}, 5); 

ich eine Funktion write_comments() in der ursprünglichen Klasse überschrieben werden soll. Diese Funktion wird ausgelöst, wenn ein in der ursprünglichen Klasse deklarierter Shortcode ausgeführt wird. Ich

In meiner Klasse haben:

class my_frontend_comment_form extends orig_frontend_comment_form { 

    public function __construct(){ 
     parent::__construct(); <--- this executes fine 

    } 

    /* 
     ** Original function in parent class that I want to overwrite: 
    */ 
    function write_comments($post_id, $results, $option, $id, $status = null) { 

     die('write_comments'); <!--- never executes 
      //do new stuff here 
     } 
    } 
}  
$comment_form = new my_frontend_comment_form(); 

write_comments() in der Klasse nie ausgelöst. Die ursprüngliche Funktion wird immer ausgeführt.

VERSUCH 1 - entfernen und dann den Shortcode erneut hinzufügen: (do_comments() ist die ursprüngliche Shortcode-Funktion)

class my_frontend_comment_form extends orig_frontend_comment_form { 

    public function __construct(){ 
     parent::__construct(); <--- this executes fine 
     remove_shortcode('orig-comment-form'); 
     add_shortcode('orig-comment-form', 'do_comments'); 
    } 

    /* 
     ** Original function in parent class that I want to overwrite: 
    */ 
    function write_comments($post_id, $results, $option, $id, $status = null) { 

     die('write_comments'); <!--- never executes 
      //do new stuff here 
     } 
    } 
}  
$comment_form = new my_frontend_comment_form(); 

VERSUCH 2 - umfasst die Shortcode-Funktion in meiner neuen Klasse

class my_frontend_comment_form extends orig_frontend_comment_form { 

    public function __construct(){ 
     parent::__construct(); <--- this executes fine 
     remove_shortcode('orig-comment-form'); 
     add_shortcode('orig-comment-form', 'my_do_comments'); 
    } 

    /* 
     ** Shortcode function 
    */ 
    function my_do_comments($atts){ 
     die('my_do_comments'); <!--- never executes 
     parent::do_comments($atts); 
    } 

    /* 
     ** Original function in parent class that I want to overwrite: 
    */ 
    function write_comments($post_id, $results, $option, $id, $status = null) { 

     die('write_comments'); <!--- never executes 
      //do new stuff here 
     } 
    } 
}  
$comment_form = new my_frontend_comment_form(); 

Weiß jemand, was ich falsch mache?

Antwort

0

Wenn Sie einen Shortcode, Filter oder eine Aktion innerhalb einer Klasse definieren. Sie können nicht einfach die Funktion übergeben. Die Funktion sollte array($this, 'function_name') sein. Also versuchen Sie es (für Versuch 2).

class my_frontend_comment_form extends orig_frontend_comment_form { 

    public function __construct(){ 
    parent::__construct(); <--- this executes fine 
    remove_shortcode('orig-comment-form'); 
    add_shortcode('orig-comment-form', array($this, 'my_do_comments')); 
    } 

    /* 
    ** Shortcode function 
    */ 
    function my_do_comments($atts){ 
    die('my_do_comments'); <!--- never executes 
    parent::do_comments($atts); 
    } 

    /* 
    ** Original function in parent class that I want to overwrite: 
    */ 
    function write_comments($post_id, $results, $option, $id, $status = null) { 

    die('write_comments'); <!--- never executes 
     //do new stuff here 
    } 
    } 
}  
$comment_form = new my_frontend_comment_form();