Why Customize WordPress Title Tags?
WordPress automatically outputs the <title> tag in the page header via the wp_head() function. To implement specific SEO strategies, branding requirements, or design needs, developers often need full control over the title generation logic and display. This requires removing the default title output and implementing a custom solution.
Method 1: Remove Default Title & Create a Custom Function
This two-step method first removes the core title rendering, then implements a custom title function in your theme.
Step 1: Remove Default Title Tag Output
Add this code to your theme's functions.php file to disable automatic title rendering in wp_head():
// Remove default title tag output from wp_head()
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
Step 2: Create & Call a Custom Title Function
After removing the default output, you must manually output the title in your theme template (typically header.php). Create a custom function for complex logic and call it within the <head> section.
// Example: A simple custom title function
function my_custom_document_title() {
$title = '';
if ( is_home() || is_front_page() ) {
$title = get_bloginfo( 'name' ) . ' - ' . get_bloginfo( 'description' );
} elseif ( is_singular() ) {
$title = get_the_title() . ' - ' . get_bloginfo( 'name' );
} elseif ( is_category() ) {
$title = single_cat_title( '', false ) . ' - Category - ' . get_bloginfo( 'name' );
} else {
$title = wp_title( '', false ) . ' - ' . get_bloginfo( 'name' );
}
echo '' . esc_html( $title ) . ' ';
}
// Call this function in header.php's <head> section:
// <?php my_custom_document_title(); ?>
Advantage: This method offers maximum flexibility, giving you full control over title concatenation rules, separators, and logic for all pages.
Method 2: Using Filters to Rewrite Archive Titles
If you primarily want to modify the title format for archive pages (categories, tags, authors) without completely overriding all title output, you can use WordPress filters. This is typically used to beautify or simplify archive titles. Note: This method does not directly control the <title> tag in <head>; it mainly affects titles output by functions like the_archive_title().
add_filter( 'get_the_archive_title', function ($title) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = '' . get_the_author() . '';
} elseif ( is_post_type_archive() ) {
$title = post_type_archive_title( '', false );
}
return $title;
});
Important: The get_the_archive_title filter modifies archive titles displayed in the page content area (e.g., "Category: XXX"). It does not directly affect the <title> tag in <head>. For global control over browser tab and SEO titles, use Method 1 or the recommended document_title_parts filter.
Recommended Method: Use the document_title_parts Filter (Best Practice)
For WordPress 4.4.0+, the standard, non-destructive way to customize titles is using the document_title_parts filter. It allows fine-grained modification of title components (site name, page title, separator) without removing core functionality.
add_filter( 'document_title_parts', function( $title_parts ) {
// Modify specific parts of the title array
if ( is_singular() ) {
$title_parts['title'] = '[Featured] ' . $title_parts['title'];
}
// Optionally modify or remove the site name
// unset( $title_parts['site'] );
// $title_parts['site'] = 'My Site';
return $title_parts;
});
This method doesn't require remove_action, maintains better compatibility with core and plugins, and is the current best practice for customizing WordPress page titles.