我有一篇内容" 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;
}
}
答案 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;
}