Jim Maguire
Jim Maguire

Reputation: 1030

How do you pass arguments through Wordpress filters and actions in OOP?

I have a post with the content "world". I want to add a filter to make it "Hello world". How to you pass arguments through Wordpress filters (and actions) if the filter is a method inside an OOP class? It should be somehow in the array argument, but I can't figure out how to format it. What about priorities?

$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; 
}

}

Upvotes: 1

Views: 663

Answers (1)

marqs
marqs

Reputation: 116

Make it an class variable and use it that way.

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; 
}

Upvotes: 2

Related Questions