pre_get_document_title
pe_pre_get_document_title dynamically modifies WordPress titles, prioritizing user engagement with personalized content hints.
This function, pe_pre_get_document_title
, acts as a filter for the document title. If the current page is a category and specifically the ‘news’ category, it appends a custom string to the title, giving users a hint about the content focus: “Get your daily dose of what’s hot in the tech and dev world.”
if( !function_exists('pe_pre_get_document_title') ) {
/**
* Filters the document title before it is generated.
* <title>Dcoument title</title>
* Change the document/page title. It will change the title in the browser's tab as well.
*
* @param string $title
*
* @return string
*/
function pe_pre_get_document_title( $title ) {
if( is_category('news') ) {
return $title . ' - Get your daily dose of what\'s hot in the tech and dev world';
}
}
add_filter( 'pre_get_document_title', 'pe_pre_get_document_title', 20, 1 );
}
The code snippet shared here showcases how developers can leverage the pre_get_document_title
hook to dynamically modify titles based on specific conditions.
In the code, the priority for the add_filter
function has been increased to 20
. This priority ensures that the pe_pre_get_document_title
hook is executed later in the WordPress execution process, allowing it to take precedence over other filters with lower priorities, including those set by Yoast SEO. By increasing the priority, you enhance the likelihood of your custom document title modification overriding any conflicting settings from other plugins.
[tweet]
When a non-empty value is passed, pe_pre_get_document_title
interrupts wp_get_document_title(), providing the specified value instantly.