WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/20/2015 04:20:04 PM (4 years ago)
Author:
obenland
Message:

Themes: Improve document title output.

Introduces more flexibility in filtering all parts of the document title,the
separator, and a way to short-circuit title generation. Plugins can now also
check for theme support and reliably filter the entire output. See #18548.
Deprecates wp_title().

Fixes #31078.

File:
1 edited

Legend:

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

    r34706 r35294  
    36363636    return force_ssl_admin( $force );
    36373637}
     3638
     3639/**
     3640 * Formerly used to display or retrieve page title for all areas of blog.
     3641 *
     3642 * By default, the page title will display the separator before the page title,
     3643 * so that the blog title will be before the page title. This is not good for
     3644 * title display, since the blog title shows up on most tabs and not what is
     3645 * important, which is the page that the user is looking at.
     3646 *
     3647 * There are also SEO benefits to having the blog title after or to the 'right'
     3648 * or the page title. However, it is mostly common sense to have the blog title
     3649 * to the right with most browsers supporting tabs. You can achieve this by
     3650 * using the seplocation parameter and setting the value to 'right'. This change
     3651 * was introduced around 2.5.0, in case backwards compatibility of themes is
     3652 * important.
     3653 *
     3654 * @since 1.0.0
     3655 * @deprecated 4.4.0
     3656 * @deprecated Use add_theme_support( 'title-tag' );
     3657 *
     3658 * @param string $sep Optional, default is '»'. How to separate the various items within the page title.
     3659 * @param bool $display Optional, default is true. Whether to display or retrieve title.
     3660 * @param string $seplocation Optional. Direction to display title, 'right'.
     3661 * @return string|null String on retrieve, null when displaying.
     3662 */
     3663function wp_title( $sep = '»', $display = true, $seplocation = '' ) {
     3664    _deprecated_function( __FUNCTION__, '4.4', 'add_theme_support( \'title-tag\' )' );
     3665
     3666    global $wp_locale;
     3667
     3668    $m        = get_query_var( 'm' );
     3669    $year     = get_query_var( 'year' );
     3670    $monthnum = get_query_var( 'monthnum' );
     3671    $day      = get_query_var( 'day' );
     3672    $search   = get_query_var( 's' );
     3673    $title    = '';
     3674
     3675    $t_sep = '%WP_TITILE_SEP%'; // Temporary separator, for accurate flipping, if necessary
     3676
     3677    // If there is a post
     3678    if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
     3679        $title = single_post_title( '', false );
     3680    }
     3681
     3682    // If there's a post type archive
     3683    if ( is_post_type_archive() ) {
     3684        $post_type = get_query_var( 'post_type' );
     3685        if ( is_array( $post_type ) ) {
     3686            $post_type = reset( $post_type );
     3687        }
     3688        $post_type_object = get_post_type_object( $post_type );
     3689        if ( ! $post_type_object->has_archive ) {
     3690            $title = post_type_archive_title( '', false );
     3691        }
     3692    }
     3693
     3694    // If there's a category or tag
     3695    if ( is_category() || is_tag() ) {
     3696        $title = single_term_title( '', false );
     3697    }
     3698
     3699    // If there's a taxonomy
     3700    if ( is_tax() ) {
     3701        $term = get_queried_object();
     3702        if ( $term ) {
     3703            $tax   = get_taxonomy( $term->taxonomy );
     3704            $title = single_term_title( $tax->labels->name . $t_sep, false );
     3705        }
     3706    }
     3707
     3708    // If there's an author
     3709    if ( is_author() && ! is_post_type_archive() ) {
     3710        $author = get_queried_object();
     3711        if ( $author ) {
     3712            $title = $author->display_name;
     3713        }
     3714    }
     3715
     3716    // Post type archives with has_archive should override terms.
     3717    if ( is_post_type_archive() && $post_type_object->has_archive ) {
     3718        $title = post_type_archive_title( '', false );
     3719    }
     3720
     3721    // If there's a month
     3722    if ( is_archive() && ! empty( $m ) ) {
     3723        $my_year  = substr( $m, 0, 4 );
     3724        $my_month = $wp_locale->get_month( substr( $m, 4, 2 ) );
     3725        $my_day   = intval( substr( $m, 6, 2 ) );
     3726        $title    = $my_year . ( $my_month ? $t_sep . $my_month : '' ) . ( $my_day ? $t_sep . $my_day : '' );
     3727    }
     3728
     3729    // If there's a year
     3730    if ( is_archive() && ! empty( $year ) ) {
     3731        $title = $year;
     3732        if ( ! empty( $monthnum ) ) {
     3733            $title .= $t_sep . $wp_locale->get_month( $monthnum );
     3734        }
     3735        if ( ! empty( $day ) ) {
     3736            $title .= $t_sep . zeroise( $day, 2 );
     3737        }
     3738    }
     3739
     3740    // If it's a search
     3741    if ( is_search() ) {
     3742        /* translators: 1: separator, 2: search phrase */
     3743        $title = sprintf( __( 'Search Results %1$s %2$s' ), $t_sep, strip_tags( $search ) );
     3744    }
     3745
     3746    // If it's a 404 page
     3747    if ( is_404() ) {
     3748        $title = __( 'Page not found' );
     3749    }
     3750
     3751    $prefix = '';
     3752    if ( ! empty( $title ) ) {
     3753        $prefix = " $sep ";
     3754    }
     3755
     3756    /**
     3757     * Filter the parts of the page title.
     3758     *
     3759     * @since 4.0.0
     3760     *
     3761     * @param array $title_array Parts of the page title.
     3762     */
     3763    $title_array = apply_filters( 'wp_title_parts', explode( $t_sep, $title ) );
     3764
     3765    // Determines position of the separator and direction of the breadcrumb
     3766    if ( 'right' == $seplocation ) { // sep on right, so reverse the order
     3767        $title_array = array_reverse( $title_array );
     3768        $title       = implode( " $sep ", $title_array ) . $prefix;
     3769    } else {
     3770        $title = $prefix . implode( " $sep ", $title_array );
     3771    }
     3772
     3773    /**
     3774     * Filter the text of the page title.
     3775     *
     3776     * @since 2.0.0
     3777     *
     3778     * @param string $title Page title.
     3779     * @param string $sep Title separator.
     3780     * @param string $seplocation Location of the separator (left or right).
     3781     */
     3782    $title = apply_filters( 'wp_title', $title, $sep, $seplocation );
     3783
     3784    // Send it out
     3785    if ( $display ) {
     3786        echo $title;
     3787    } else {
     3788        return $title;
     3789    }
     3790}
Note: See TracChangeset for help on using the changeset viewer.