Wordpress - Issues with title-tag and document_title_parts

I ran your filter in my development area. It didn't work. Then I switched off the Yoast SEO plugin, which I knew was also messing with the page title. Then it worked. So my suggestion would be another plugin is messing with it.

In the case of Yoast, it was a filter call to pre_get_document_title returning non empty. In that case wp_get_document_title is short circuited and the rest of the function, including the documents_title_parts filter, is not evaluated, as you can see from the first lines of code:

$title = apply_filters( 'pre_get_document_title', '' );
if ( ! empty( $title ) ) {
    return $title;
    }

So, I took your filter and changed the hook to pre_get_document_title. It didn't work. Then I changed the priority to a higher level than the same filter in Yoast. Then it worked. So, I don't know about your set-up, but I suggest you give this a try:

add_filter( 'pre_get_document_title', function( $title )
  {
    error_log('here');
    return $title;
  }, 999, 1 );

After some experimenting I came to the following suggestion: could it be, that the <title> tag is "hard coded" inside your parent theme's header.php? If that is the case, you could try to remove the <title> tag from your child theme's header.php (copy your parent's header.php into your child theme folder) and then add the theme support back through the functions.php:

add_theme_support( 'title-tag' );

I'll try to explain what lead me to this suggestion: I tried as you and others suggested – but it turned out that I found two <title> tags in the source code. The first one had the standard title, the second one the modified title. But (of course) in the browser title bar I could only see the default title.

I then checked the header.php of the parent theme I used (twentyfourteen) and the <title> tag was indeed hard coded inside that template like this:

<title><?php wp_title( '|', true, 'right' ); ?></title>

After I removed it, I added the following code to the child theme's functions.php and it worked:

/**
 * Theme support added
 */

function add_theme_support_child() {

    add_theme_support( 'title-tag' );

}

add_action( 'after_setup_theme', 'add_theme_support_child', 11 );


/**
 * Change the title of a page
 * 
 */

function change_title_for_a_template( $title ) {

// Check if current page template is 'template-homepage.php'
// if ( is_page_template( 'template-homepage.php' ) ) {

    // change title parts here
    $title['title'] = 'My Title'; 
    $title['tagline'] = 'My fancy tagline'; // optional
    $title['site'] = 'example.org'; //optional

// }

return $title; 

}

add_filter( 'document_title_parts', 'change_title_for_a_template', 10, 1 );

So it basically also worked before removing the <title> tag from the template – only that there were then two <title> tags of which the later was ignored. Could this be the same problem with your theme?

Since wp 4.4.0 however the <title> tag is created dynamically by the function _wp_render_title_tag() which basically calls another function wp_get_document_title() and wraps the html tags around the result. Long story short: if your theme's header.php is missing the <title> tag, chances are that you can override the title directly through pre_get_document_title or document_title_parts as described here:

1) change the title directly:

add_filter('pre_get_document_title', 'change_the_title');
function change_the_title() {
    return 'The expected title';
}

2) filtering the title parts:

add_filter('document_title_parts', 'filter_title_part');
function filter_title_part($title) {
    return array('a', 'b', 'c');
}

Having read your post from top to botom and bottom to top, you have in all probabilty a filter that is passing a title through the pre_get_document_title filter. The clue here the following statement:

I've also tried pre_get_document_title, which does fire on page load,

Looking at the soure code for wp_get_document_title(), we see the following code:

$title = apply_filters( 'pre_get_document_title', '' );
if ( ! empty( $title ) ) {
    return $title;
}

What this means is, whenever a non empty value is passes through the pre_get_document_title filter, the wp_get_document_title() function will return whatever value that was passed via the pre_get_document_title filter. It this case, the document_title_separator filter and the document_title_parts filter will never be executed as these only run after the pre_get_document_title filter.

Looking at what you said a bit further on:

...though I'm unable to get it to actually change the title.

you definitely have a pre_get_document_title filter with authority which is overriding your instance of the same filter, and because of this filter, the function return whatever is passed to it which results in your document_title_parts filter not being executed.

What you will need to do is is to use either grep or a good editor and search your entire wp-content folder for that pre_get_document_title filter. Once you located that filter, you can take it from there to remove that filter and replace it with your own