I need to refactor some of the legacy code we're using on a wordpress project to object oriented. One area where there's a lot of duplication of code is when setting the title of the page, as you can see below, within the setTitle function.
Nested functions don't seem to work in PHP. However, assuming it would work, the below code, should accomplish what I am trying to do.
In the setTitle function, all I am trying to do is take in a string and execute the code within somehow and have it successfully call add_filter with the string that was passed in, which will successfully set the title. What would be the best way to accomplish this?
MyControllerClass.php
public function setTitle($title) {
function t($t) {
$t['title'] = $title; return $t;
}
add_filter( 'document_title_parts', 't' );
return $this;
}
Then, later in a different class..
$controller->setTitle('My Homepage')->renderHomePage();