I am working on a site where the page titles are generated dynamically from external data sources. So obviously, Yoast SEO/WordPress SEO doesn´t know anything about the correct title, description, image etc. in the head when it outputs OpenGraph tags and more.
That should be no problem so far, as WordPress SEO supports filters to hook into the output. You can find them in this list.
Filters must go into the functions.php
, I need to pass my generated data to it and then the hooks will do their magic. As I can´t use variables in add_filter()
I need some way around.
I am already using this technique described by toscho to save data in static variables, so I can access them from around the whole WordPress. (This works fine for everything, except my filters.)
So my page template is calling
$saved = title_storage($event_title);
to save the dynamically generated page title into a static variable for later use.
In my functions.php
I got the following:
function seo_change_title( $string )
{
$string = title_storage();
return $string;
}
add_filter( 'wpseo_title', 'seo_change_title', 10, 1 );
Obviously this doesn´t work. The title
is stripped from the OpenGraph tags but that´s everything that changes. Although that probably means, my variable is empty.
Trying to get my head around anonymous functions and searching WPSE for solutions, but I thought it could be this easy. Am I getting something wrong here?