Title Tags in 4.1

For over three years we have been trying to make it easier for plugins and themes to manage the document title. Kubrick didn’t necessarily set a great example to theme authors by appending the blogblog (versus network, site) name to wp_title(), a practice we have been trying to correct ever since.

#18548 was created to find a solution to that problem, but after initial excitement hasn’t seen any noteworthy action until a few weeks ago. Yesterday @johnbillion committed a first step towards a brighter future in [30074], introducing a forward compatible way to make document titles fully customizable.

Adding titles to themes

Starting with 4.1 and Twenty Fifteen, the recommended way for themes to display titles is by adding theme support like this:

function theme_slug_setup() {
   add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'theme_slug_setup' );

Support should be added on the after_setup_theme or init action, but no later than that. It does not accept any further arguments.

By declaring support like this, themes acknowledge that they are not defining titles on their own and WordPress can add it safely without duplication.

To maintain full forward compatibility, plugins can not check for theme support of title tags, and are discouraged from building functionality around it just yet. The long term plan is to enable users to manage document titles from their adminadmin (and super admin), independent of which theme they’re using. At that time it will also become more pluginPlugin A plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. These can be free in the WordPress.org Plugin Directory https://wordpress.org/plugins/ or can be cost-based plugin from a third-party friendly. To make sure this can be achieved however, it was important to rule out backwards compatibility concerns as much as possible.

While there is no consensus on how the final implementation will look like yet, this should be a good way to get themes started to opt into a more user friendly way. It will also make any future changes that much more impactful when the final version ships.

Backwards compatibility

To enable support in existing themes without breaking backwards compatibility, theme authors can check if the callback function exists, and add a shiv in case it does not:

if ( ! function_exists( '_wp_render_title_tag' ) ) :
	function theme_slug_render_title() {
?>
<title><?php wp_title( '|', true, 'right' ); ?></title>
<?php
	}
	add_action( 'wp_head', 'theme_slug_render_title' );
endif;

This would also be the place to optionally add a filterFilter Filters are one of the two types of Hooks https://codex.wordpress.org/Plugin_API/Hooks. They provide a way for functions to modify data of other functions. They are the counterpart to Actions. Unlike Actions, filters are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output. to enhance the document title, along the lines of what recent default themes have been doing.

#4-1, #bundled-theme, #dev-notes, #themes, #twentyfifteen