如何通过OOP中的Wordpress过滤器和操作传递参数?

时间:2014-08-08 23:49:30

标签: php wordpress oop filter

我有一篇内容" world"。我想添加一个过滤器来使它成为" Hello world"。如果过滤器是OOP类中的方法,如何通过Wordpress过滤器(和操作)传递参数?它应该以某种方式在数组参数中,但我无法弄清楚如何格式化它。优先事项呢?

$helloword = new HelloWorldClass;
class HelloWorldClass{

function __construct(){
    //WHERE DO I PUT THE VARIABLE $the_word_hello SO I CAN PASS IT TO THE METHOD?
    $the_word_hello = "Hello ";
    add_filter( 'the_content', array($this, 'AddHelloToTheContent') );
}

//I ASSUME THIS LINE SHOULD BE RE-WRITTEN LIKE THIS:
//public function AddHelloToTheContent($the_content, $the_word_hello){
public function AddHelloToTheContent($the_content){
    $the_content = $the_word_hello . $the_content;
    return $the_content; 
}

}

1 个答案:

答案 0 :(得分:2)

使它成为一个类变量并以这种方式使用它。

function __construct(){
    //WHERE DO I PUT THE VARIABLE $the_word_hello SO I CAN PASS IT TO THE METHOD?
    $this->the_word_hello = "Hello ";
    add_filter( 'the_content', array($this, 'AddHelloToTheContent') );
}
//I ASSUME THIS LINE SHOULD BE RE-WRITTEN LIKE THIS:
//public function AddHelloToTheContent($the_content, $the_word_hello){
public function AddHelloToTheContent($the_content){
    $the_content = $this->the_word_hello . $the_content;
    return $the_content; 
}