Introduce a new means of outputting a `<title>` tag in the theme head. Requires a theme to add support by calling `add_theme_support( 'title-tag' )`. This is the first step in adding a more robust means of generating and outputting the title tag.

See #18548.
Props obenland, chrisbliss18, joostdevalk.



git-svn-id: https://develop.svn.wordpress.org/trunk@30074 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2014-10-28 21:11:11 +00:00
parent 1e8635fcea
commit efb9cd0cc5
3 changed files with 51 additions and 1 deletions

View File

@ -196,6 +196,7 @@ add_filter( 'title_save_pre', 'trim' );
add_filter( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 );
// Actions
add_action( 'wp_head', '_wp_render_title_tag', 1 );
add_action( 'wp_head', 'wp_enqueue_scripts', 1 );
add_action( 'wp_head', 'feed_links', 2 );
add_action( 'wp_head', 'feed_links_extra', 3 );

View File

@ -730,6 +730,25 @@ function get_bloginfo( $show = '', $filter = 'raw' ) {
return $output;
}
/**
* Display <title> tag with contents.
*
* @since 4.1.0
* @access private
*/
function _wp_render_title_tag() {
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
// This can only work internally on wp_head.
if ( ! did_action( 'wp_head' ) && ! doing_action( 'wp_head' ) ) {
return;
}
echo '<title>' . wp_title( '|', false, 'right' ) . "</title>\n";
}
/**
* Display or retrieve page title for all areas of blog.
*
@ -753,7 +772,7 @@ function get_bloginfo( $show = '', $filter = 'raw' ) {
* @return string|null String on retrieve, null when displaying.
*/
function wp_title($sep = '&raquo;', $display = true, $seplocation = '') {
global $wp_locale;
global $wp_locale, $page, $paged;
$m = get_query_var('m');
$year = get_query_var('year');
@ -853,6 +872,19 @@ function wp_title($sep = '&raquo;', $display = true, $seplocation = '') {
$title = $prefix . implode( " $sep ", $title_array );
}
if ( current_theme_supports( 'title-tag' ) && ! is_feed() ) {
$title .= get_bloginfo( 'name', 'display' );
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) ) {
$title .= " $sep $site_description";
}
if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
$title .= " $sep " . sprintf( __( 'Page %s' ), max( $paged, $page ) );
}
}
/**
* Filter the text of the page title.
*

View File

@ -1611,6 +1611,15 @@ function add_theme_support( $feature ) {
define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
break;
// Ensure that 'title-tag' is accessible in the admin.
case 'title-tag' :
// Can be called in functions.php but must happen before wp_loaded, i.e. not in header.php.
if ( did_action( 'wp_loaded' ) ) {
_doing_it_wrong( "add_theme_support( 'title-tag' )", sprintf( _x( 'You need to add theme support before %s.', 'action name' ), '<code>wp_loaded</code>' ), '4.1.0' );
return false;
}
}
$_wp_theme_features[ $feature ] = $args;
@ -1763,6 +1772,14 @@ function current_theme_supports( $feature ) {
if ( !isset( $_wp_theme_features[$feature] ) )
return false;
if ( 'title-tag' == $feature ) {
// Don't confirm support unless called internally.
$trace = debug_backtrace();
if ( ! in_array( $trace[1]['function'], array( '_wp_render_title_tag', 'wp_title' ) ) ) {
return false;
}
}
// If no args passed then no extra checks need be performed
if ( func_num_args() <= 1 )
return true;