2016-04-15 60 views
1

我想擴展我的孩子主題內的第三方插件類。WordPress - 如何擴展第三方插件類

我包括我的使用init鉤新的類,它工作得很好:

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

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

}, 5); 

我想重寫函數write_comments()在原始類。 此函數在原​​始類中聲明的簡碼執行時觸發。

在我的課,我有:

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()在我的課永遠不會觸發。原始功能始終執行。

未遂1 - 刪除,然後重新添加簡碼: (do_comments()是原來的簡碼功能)

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

未遂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', '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(); 

有誰知道我在做什麼錯?

回答

0

如果您正在定義類中的短代碼,過濾器或操作。你不能只是傳遞函數。該功能應該是array($this, 'function_name')。所以試試這個(更改嘗試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();