Monday, 16 October 2017

Wordpress: Change the title of any custom page dynamically

Sometime we want to custom the title for some page eg: 404, contact, search .... It is very simple, follow these instructions.

in file "your_theme/function.php" add the following code at the end.

function setTitle($title) {
    add_filter( 'wp_title', function() use ($title) {return $title;}, 10000 );
}

Done ! From now on, which page should be customized so that you just call the function like this:

setTitle('Hello, this is my custom title');

It does not affect the pages you do not use. Have fun!