WordPress - 如何扩展第三方插件类

时间:2016-04-15 02:53:01

标签: wordpress class plugins shortcode

我正在尝试在我的子主题中扩展第三方插件类。

我使用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()永远不会开火。始终执行原始功能。

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

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

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分: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();