In the realm of WordPress, altering the title of a page dynamically can be an essential customization to better reflect the content or the branding of the site. The following PHP snippet demonstrates how to change the page title using a WordPress filter hook.
Code
<?php
add_filter('pre_get_document_title', function($title) {
if (is_post_type_archive('species')) {
$title = __("Humans");
}
return $title;
}, 9999);
This snippet leverages the pre_get_document_title filter, which allows developers to modify the page title before it is retrieved:
Use Case
This snippet is particularly useful when you have a custom post type archive in WordPress and you wish to display a customized title for it that is different from the default or dynamically generated one. In this case, whenever visitors navigate to the archive page for ‘species’, they will see “Humans” as the page title.

Leave a Reply