The document_title WordPress PHP filter allows you to modify the document title.
Usage
add_filter( 'document_title', 'custom_document_title' ); function custom_document_title( $title ) { // your custom code here return $title; }
Parameters
- $title (string) – The document title to be filtered.
More information
See WordPress Developer Resources: document_title
Examples
Add a custom prefix to the document title
Add a custom prefix to the document title for branding purposes.
add_filter( 'document_title', 'custom_prefix_document_title' ); function custom_prefix_document_title( $title ) { $prefix = 'MyBrand: '; return $prefix . $title; }
Add the current date to the document title
Display the current date at the beginning of the document title.
add_filter( 'document_title', 'date_prefix_document_title' ); function date_prefix_document_title( $title ) { $date = date('F j, Y'); return $date . ' - ' . $title; }
Make the document title all uppercase
Transform the document title to uppercase.
add_filter( 'document_title', 'uppercase_document_title' ); function uppercase_document_title( $title ) { return strtoupper( $title ); }
Add a custom suffix to the document title
Add a custom suffix to the document title for additional context.
add_filter( 'document_title', 'custom_suffix_document_title' ); function custom_suffix_document_title( $title ) { $suffix = ' - Official Site'; return $title . $suffix; }
Remove specific words from the document title
Remove specific words from the document title for better SEO.
add_filter( 'document_title', 'remove_words_document_title' ); function remove_words_document_title( $title ) { $remove_words = array( 'example', 'test' ); $title_array = explode( ' ', $title ); $filtered_title_array = array_diff( $title_array, $remove_words ); return implode( ' ', $filtered_title_array ); }