Ticket #18548: title-tag.diff

File title-tag.diff, 7.6 KB (added by joostdevalk, 21 months ago)

Patch to add title tag functionality

  • wp-includes/default-filters.php

     
    204204add_action( 'wp_head',             'rsd_link'                               ); 
    205205add_action( 'wp_head',             'wlwmanifest_link'                       ); 
    206206add_action( 'wp_head',             'index_rel_link'                         ); 
     207add_action( 'wp_head',                     '_wp_render_title_tag',                        5             ); 
    207208add_action( 'wp_head',             'parent_post_rel_link',            10, 0 ); 
    208209add_action( 'wp_head',             'start_post_rel_link',             10, 0 ); 
    209210add_action( 'wp_head',             'adjacent_posts_rel_link_wp_head', 10, 0 ); 
  • wp-includes/general-template.php

     
    617617} 
    618618 
    619619/** 
     620 * Display <title> tag with contents 
     621 *  
     622 * When using add_theme_support to add support for this function, one can set a few variables: 
     623 * 'sep' string, for the separator used in title tags 
     624 * 'home-title' string, for the homepage title. Only used for the blog page when the blog page is also the frontpage. 
     625 * 'frontpage-title' string, for the front page when it's not the posts page. 
     626 * 'show-sitename' boolean, defaults to true. When set to false the site name will not be added to titles. 
     627 * 
     628 * @since 3.3 
     629 */ 
     630function _wp_render_title_tag() { 
     631        if ( ! current_theme_supports( 'title-tag' ) ) 
     632                return; 
     633         
     634        // Allow early filtering, if this returns a title skip all the logic below and print it, if it returns false, bail completely 
     635        if ( null !== $pre = apply_filters( 'pre_wp_title_tag', null ) ) { 
     636                if ( $pre ) 
     637                        echo '<title>' . $pre . "<title>\n"; 
     638                return; 
     639        } 
     640 
     641        global $page, $paged, $wp_locale; 
     642         
     643        // Options that can be passed along: 
     644        $options = get_theme_support( 'title-tag' ); 
     645        $options = $options[0]; 
     646 
     647        $show_sitename = true; 
     648        if ( isset( $options['show-sitename'] ) ) 
     649                $show_sitename = $options['show-sitename']; 
     650                 
     651        $title = array(); 
     652         
     653        $m = get_query_var('m'); 
     654        $year = get_query_var('year'); 
     655        $monthnum = get_query_var('monthnum'); 
     656        $day = get_query_var('day'); 
     657 
     658        if ( is_home() && is_front_page() ) { 
     659                if ( isset($options['home-title']) && $options['home-title'] ) { 
     660                        $title[] = $options['home-title']; 
     661                } else { 
     662                        $title[] = get_bloginfo('name'); 
     663 
     664                        // Add a page number if necessary: 
     665                        if ( $paged >= 2 || $page >= 2 ) 
     666                                $title[] = sprintf( __( 'Page %s' ), max( $paged, $page ) ); 
     667 
     668                        if ( '' != $desc = get_bloginfo( 'description', 'display' ) ) 
     669                                $title[] = $desc;  
     670                } 
     671        // If we're on the blog page and that page is not the homepage, use the container page's title 
     672        } elseif ( is_home() && !is_front_page() ) { 
     673                $_post = get_post( get_option('page_for_posts') ); 
     674                $title[] = $_post->post_title; 
     675 
     676                // Add a page number if necessary: 
     677                if ( $paged >= 2 || $page >= 2 ) 
     678                        $title[] = sprintf( __( 'Page %s' ), max( $paged, $page ) ); 
     679 
     680                if ( $show_sitename ) 
     681                        $title[] = get_bloginfo('name'); 
     682        } elseif ( !is_home() && is_front_page() ) { 
     683                if ( isset( $options['frontpage-title'] ) && $options['frontpage-title'] ) { 
     684                        $title[] = $options['frontpage-title']; 
     685                } else { 
     686                        $title[] = single_post_title( '', false ); 
     687 
     688                        if ( $show_sitename ) 
     689                                $title[] = get_bloginfo('name'); 
     690                } 
     691        } else { 
     692                // If we're on a post / page 
     693                if ( is_singular() ) { 
     694                        $title[] = single_post_title( '', false ); 
     695                } 
     696                 
     697                // If we're on a category or tag archive 
     698                elseif ( is_category() || is_tag() ) { 
     699                        $title[] = single_term_title( '', false ); 
     700                } 
     701                 
     702                // If we're on a taxonomy archive 
     703                elseif ( is_tax() ) { 
     704                        $term = get_queried_object(); 
     705                        $tax = get_taxonomy( $term->taxonomy ); 
     706                        $title[] = single_term_title( $tax->labels->name, false ); 
     707                } 
     708 
     709                // If we're on an author archive 
     710                elseif ( is_author() ) { 
     711                        $author = get_queried_object(); 
     712                        $title[] = $author->display_name; 
     713                } 
     714 
     715                // If we're on a post type archive 
     716                elseif ( is_post_type_archive() ) { 
     717                        $title[] = post_type_archive_title( '', false ); 
     718                } 
     719                 
     720                // If it's a date archive 
     721                elseif ( is_archive() && !empty($year) ) { 
     722                        $t = ''; 
     723                        if ( !empty($monthnum) ) { 
     724                                $month = $wp_locale->get_month($monthnum); 
     725                                if ( !empty($day) ) 
     726                                        $t .= zeroise($day, 2) . ' '; 
     727                                $t .= $month . ' '; 
     728                        }  
     729                        $title[] = $t . $year; 
     730                } 
     731 
     732                // If it's a search 
     733                elseif ( is_search() ) { 
     734                        /* translators: 1: search phrase */ 
     735                        $title[] = sprintf(__('Search Results for "%1$s"'), strip_tags( get_query_var('s') ) ); 
     736                } 
     737 
     738                // If it's a 404 page 
     739                elseif ( is_404() ) { 
     740                        $title[] = __('Page not found'); 
     741                } 
     742                 
     743                // Add a page number if necessary: 
     744                if ( $paged >= 2 || $page >= 2 ) 
     745                        $title[] = sprintf( __( 'Page %s' ), max( $paged, $page ) ); 
     746                 
     747                if ( $show_sitename ) 
     748                        $title[] = get_bloginfo('name'); 
     749        } 
     750        $title = apply_filters( 'wp_title_tag_array', $title );  
     751 
     752        // sep, defaults to - as the separator between two parts of the title 
     753        $sep = '-'; 
     754        if ( isset($options['sep']) ) 
     755                $sep = $options['sep']; 
     756 
     757        $title = implode( " $sep ", $title ); 
     758         
     759        $title = apply_filters( 'wp_title_tag', $title, $sep );  
     760        echo "<title>" . $title . "</title>\n"; 
     761} 
     762 
     763/** 
    620764 * Display or retrieve page title for post. 
    621765 * 
    622766 * This is optimized for single.php template file for displaying the post title. 
  • wp-content/themes/twentyeleven/header.php

     
    2424<head> 
    2525<meta charset="<?php bloginfo( 'charset' ); ?>" /> 
    2626<meta name="viewport" content="width=device-width" /> 
    27 <title><?php 
    28         /* 
    29          * Print the <title> tag based on what is being viewed. 
    30          */ 
    31         global $page, $paged; 
    32  
    33         wp_title( '|', true, 'right' ); 
    34  
    35         // Add the blog name. 
    36         bloginfo( 'name' ); 
    37  
    38         // Add the blog description for the home/front page. 
    39         $site_description = get_bloginfo( 'description', 'display' ); 
    40         if ( $site_description && ( is_home() || is_front_page() ) ) 
    41                 echo " | $site_description"; 
    42  
    43         // Add a page number if necessary: 
    44         if ( $paged >= 2 || $page >= 2 ) 
    45                 echo ' | ' . sprintf( __( 'Page %s', 'twentyeleven' ), max( $paged, $page ) ); 
    46  
    47         ?></title> 
    4827<link rel="profile" href="http://gmpg.org/xfn/11" /> 
    4928<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" /> 
    5029<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" /> 
  • wp-content/themes/twentyeleven/functions.php

     
    109109        // This theme uses Featured Images (also known as post thumbnails) for per-post/per-page Custom Header images 
    110110        add_theme_support( 'post-thumbnails' ); 
    111111 
     112        // Add support for Title tags 
     113        // sep is the separator between parts of the title 
     114        // show-sitename determines whether the sitename is shown (at the end of the title) 
     115        // home-title sets the title for the homepage, which defaults to SITE NAME -sep- SITE DESCRIPTION 
     116        // frontpage-title is used when the frontpage does not display the latest posts but a page instead 
     117        add_theme_support( 'title-tag', array( 'sep' => '-', 'show-sitename' => true, 'home-title' => false, 'frontpage-title' => get_bloginfo('name') ) ); 
     118 
    112119        // The next four constants set how Twenty Eleven supports custom headers. 
    113120 
    114121        // The default header text color