Make WordPress Core


Ignore:
Timestamp:
11/11/2015 11:49:31 PM (11 years ago)
Author:
obenland
Message:

Template: Un-deprecate wp_title().

Before it can be deprecated we should identify alternative usages and define
a path forward for them.

See [35294], #31078.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r35572 r35624  
    955955
    956956        echo '<title>' . wp_get_document_title() . '</title>' . "\n";
     957}
     958
     959/**
     960 * Display or retrieve page title for all areas of blog.
     961 *
     962 * By default, the page title will display the separator before the page title,
     963 * so that the blog title will be before the page title. This is not good for
     964 * title display, since the blog title shows up on most tabs and not what is
     965 * important, which is the page that the user is looking at.
     966 *
     967 * There are also SEO benefits to having the blog title after or to the 'right'
     968 * or the page title. However, it is mostly common sense to have the blog title
     969 * to the right with most browsers supporting tabs. You can achieve this by
     970 * using the seplocation parameter and setting the value to 'right'. This change
     971 * was introduced around 2.5.0, in case backwards compatibility of themes is
     972 * important.
     973 *
     974 * @since 1.0.0
     975 *
     976 * @global WP_Locale $wp_locale
     977 *
     978 * @param string $sep         Optional, default is '&raquo;'. How to separate the various items
     979 *                            within the page title.
     980 * @param bool   $display     Optional, default is true. Whether to display or retrieve title.
     981 * @param string $seplocation Optional. Direction to display title, 'right'.
     982 * @return string|null String on retrieve, null when displaying.
     983 */
     984function wp_title( $sep = '&raquo;', $display = true, $seplocation = '' ) {
     985        global $wp_locale;
     986
     987        $m        = get_query_var( 'm' );
     988        $year     = get_query_var( 'year' );
     989        $monthnum = get_query_var( 'monthnum' );
     990        $day      = get_query_var( 'day' );
     991        $search   = get_query_var( 's' );
     992        $title    = '';
     993
     994        $t_sep = '%WP_TITILE_SEP%'; // Temporary separator, for accurate flipping, if necessary
     995
     996        // If there is a post
     997        if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
     998                $title = single_post_title( '', false );
     999        }
     1000
     1001        // If there's a post type archive
     1002        if ( is_post_type_archive() ) {
     1003                $post_type = get_query_var( 'post_type' );
     1004                if ( is_array( $post_type ) ) {
     1005                        $post_type = reset( $post_type );
     1006                }
     1007                $post_type_object = get_post_type_object( $post_type );
     1008                if ( ! $post_type_object->has_archive ) {
     1009                        $title = post_type_archive_title( '', false );
     1010                }
     1011        }
     1012
     1013        // If there's a category or tag
     1014        if ( is_category() || is_tag() ) {
     1015                $title = single_term_title( '', false );
     1016        }
     1017
     1018        // If there's a taxonomy
     1019        if ( is_tax() ) {
     1020                $term = get_queried_object();
     1021                if ( $term ) {
     1022                        $tax   = get_taxonomy( $term->taxonomy );
     1023                        $title = single_term_title( $tax->labels->name . $t_sep, false );
     1024                }
     1025        }
     1026
     1027        // If there's an author
     1028        if ( is_author() && ! is_post_type_archive() ) {
     1029                $author = get_queried_object();
     1030                if ( $author ) {
     1031                        $title = $author->display_name;
     1032                }
     1033        }
     1034
     1035        // Post type archives with has_archive should override terms.
     1036        if ( is_post_type_archive() && $post_type_object->has_archive ) {
     1037                $title = post_type_archive_title( '', false );
     1038        }
     1039
     1040        // If there's a month
     1041        if ( is_archive() && ! empty( $m ) ) {
     1042                $my_year  = substr( $m, 0, 4 );
     1043                $my_month = $wp_locale->get_month( substr( $m, 4, 2 ) );
     1044                $my_day   = intval( substr( $m, 6, 2 ) );
     1045                $title    = $my_year . ( $my_month ? $t_sep . $my_month : '' ) . ( $my_day ? $t_sep . $my_day : '' );
     1046        }
     1047
     1048        // If there's a year
     1049        if ( is_archive() && ! empty( $year ) ) {
     1050                $title = $year;
     1051                if ( ! empty( $monthnum ) ) {
     1052                        $title .= $t_sep . $wp_locale->get_month( $monthnum );
     1053                }
     1054                if ( ! empty( $day ) ) {
     1055                        $title .= $t_sep . zeroise( $day, 2 );
     1056                }
     1057        }
     1058
     1059        // If it's a search
     1060        if ( is_search() ) {
     1061                /* translators: 1: separator, 2: search phrase */
     1062                $title = sprintf( __( 'Search Results %1$s %2$s' ), $t_sep, strip_tags( $search ) );
     1063        }
     1064
     1065        // If it's a 404 page
     1066        if ( is_404() ) {
     1067                $title = __( 'Page not found' );
     1068        }
     1069
     1070        $prefix = '';
     1071        if ( ! empty( $title ) ) {
     1072                $prefix = " $sep ";
     1073        }
     1074
     1075        /**
     1076         * Filter the parts of the page title.
     1077         *
     1078         * @since 4.0.0
     1079         *
     1080         * @param array $title_array Parts of the page title.
     1081         */
     1082        $title_array = apply_filters( 'wp_title_parts', explode( $t_sep, $title ) );
     1083
     1084        // Determines position of the separator and direction of the breadcrumb
     1085        if ( 'right' == $seplocation ) { // sep on right, so reverse the order
     1086                $title_array = array_reverse( $title_array );
     1087                $title       = implode( " $sep ", $title_array ) . $prefix;
     1088        } else {
     1089                $title = $prefix . implode( " $sep ", $title_array );
     1090        }
     1091
     1092        /**
     1093         * Filter the text of the page title.
     1094         *
     1095         * @since 2.0.0
     1096         *
     1097         * @param string $title Page title.
     1098         * @param string $sep Title separator.
     1099         * @param string $seplocation Location of the separator (left or right).
     1100         */
     1101        $title = apply_filters( 'wp_title', $title, $sep, $seplocation );
     1102
     1103        // Send it out
     1104        if ( $display ) {
     1105                echo $title;
     1106        } else {
     1107                return $title;
     1108        }
    9571109}
    9581110
Note: See TracChangeset for help on using the changeset viewer.