07-27-2023, 08:43 AM
I have created a WP plugin which uses the query string to pull in page data based on what the visitor has selected. Obviously this 'simulates' additional pages but the page title does not change from the title set in WP Admin.
I have been trying to hook into `wp_title` to change the title tag on fly but can't get this one working.
The following function works:
public function custom_title($title) {
return 'new title';
}
add_filter( 'wp_title', array($this, 'custom_title'), 20 );
// changes <title> to 'new title'
As soon as I try to pass a variable to it, it fails.
public function custom_title($title, $new_title) {
return $new_title;
}
WordPress complains it's missing the 2nd argument, I guess this makes sense since the function is being called at page load... I was hoping I could do something like `$this->custom_title($title, 'new title);` within my plugin but it doesn't look like that is going to be possible?
I have posted this here because I think it's a general PHP class issue.
Can I globalise a returned variable, e.g. I want to return the 'title' column from a query in another function such as `$query->title`
When the function runs it returns data from the database
public function view_content()
{
$query = $this->db->get_row('SELECT title FROM ...');
$query->title;
}
I now need $query->title to be set as the page title.
public function custom_title()
{
if($query->title)
{
$new_title = $query->title;
}
}
I have been trying to hook into `wp_title` to change the title tag on fly but can't get this one working.
The following function works:
public function custom_title($title) {
return 'new title';
}
add_filter( 'wp_title', array($this, 'custom_title'), 20 );
// changes <title> to 'new title'
As soon as I try to pass a variable to it, it fails.
public function custom_title($title, $new_title) {
return $new_title;
}
WordPress complains it's missing the 2nd argument, I guess this makes sense since the function is being called at page load... I was hoping I could do something like `$this->custom_title($title, 'new title);` within my plugin but it doesn't look like that is going to be possible?
I have posted this here because I think it's a general PHP class issue.
Can I globalise a returned variable, e.g. I want to return the 'title' column from a query in another function such as `$query->title`
When the function runs it returns data from the database
public function view_content()
{
$query = $this->db->get_row('SELECT title FROM ...');
$query->title;
}
I now need $query->title to be set as the page title.
public function custom_title()
{
if($query->title)
{
$new_title = $query->title;
}
}