PHP function return not working


Hi all, pretty much a newbie to PHP so please be gentle and keep it simple…

I am trying to change the browser tab title “on the fly” for certain WP pages.

Following some ideas elsewhere I have the following:-

In the content-page.php of my template I have added

global $new_title;
$new_title = 'My New Title';
change_title ();

In functions.php I have
add_filter('pre_get_document_title', 'change_title');

function change_title () {
global $new_title;

var_dump ($new_title);
	 return $new_title;

This doesn’t work. Although var_dump shows a correct string the tab title is not changed.

However, if I change the return statement to

return "abcdefg";

Then the tab title is updated correctly.

I know it must be something bleedingly obvious, but I have spent hours on this trying all sorts of things. Any help greatly appreciated. Thanks