Declare global var from Template File and use it in Functions.php

I am using the wpseo_title filter to rewrite the authors page titles from my site.

I have the following code in author.php template:

   global $author_page_titles;
   $author_page_titles = $curauth->nickname .', Preparador de Oposiciones a '. $term_especialidad->name .' en '. $term_region->name;

Which outputs Kevin, Preparador de Oposiciones a Forestales en Madrid in my author page (in other pages is the given author nickname and his/her selected terms).

In my functions.php I have the following function:

function my_custom_authorpage_title($author_page_titles) {
  if (is_author()) {
    global $author_page_titles;
    return $author_page_titles;
add_filter('wpseo_title', 'my_custom_authorpage_title', 100);

I’m pretty sure that the mistake must be in how and where I declare the var, but can’t find the solution.

1 Answer

You can use the exact same filter in your template files. If you do need it in functions.php for any reason (maybe you have some additional processing) then you can use your own custom filter.


function my_custom_authorpage_title( $title ) {
    // process ...
    return apply_filters( 'my_title', $title );
add_filter( 'wpseo_title', 'my_custom_authorpage_title' );

author.php (before get_header()):

add_filter( 'my_title', function( $title ) use ( $curauth, $term_especialidad, $term_region ) {
    return $curauth->nickname . '...';

Hope that helps!

Leave a Comment