| | 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.4.0 |
| | 629 | * @access private |
| | 630 | */ |
| | 631 | function _wp_render_title_tag() { |
| | 632 | if ( ! current_theme_supports( 'title-tag' ) ) |
| | 633 | return; |
| | 634 | |
| | 635 | // This can only work internally on wp_head. |
| | 636 | if ( ! did_action( 'wp_head' ) && ( ! function_exists( 'doing_action' ) || ! doing_action( 'wp_head' ) ) ) |
| | 637 | return; |
| | 638 | |
| | 639 | // If wp_title() has fired, don't do anything. |
| | 640 | if ( did_action( 'wp_title' ) ) |
| | 641 | return; |
| | 642 | |
| | 643 | // Allow early filtering, if this returns a title skip all the logic below |
| | 644 | // and print it. |
| | 645 | if ( null !== $pre = apply_filters( 'pre_wp_title_tag', null ) ) { |
| | 646 | echo "<title>$pre</title>\n"; |
| | 647 | return; |
| | 648 | } |
| | 649 | |
| | 650 | global $page, $paged; |
| | 651 | |
| | 652 | // Retrieve the options set by the add_theme_support call. The |
| | 653 | // title_tag_options_filter filter allows any interested code to modify the |
| | 654 | // set options easily. |
| | 655 | $options = get_theme_support( 'title-tag' ); |
| | 656 | $options = apply_filters( 'title_tag_options_filter', $options[0] ); |
| | 657 | |
| | 658 | // ltr language sites tend to use title that has the most-specific part of |
| | 659 | // the title on the left. rtl language sites tend to reverse this. The auto |
| | 660 | // option automatically selects the appropriate direction based upon whether |
| | 661 | // the site is set to use a text direction of ltr or rtl. This can be |
| | 662 | // directly overridden by supplying ltr or rtl. |
| | 663 | if ( 'auto' == $options['direction'] || ! in_array( $options['direction'], array( 'ltr', 'rtl' ) ) ) |
| | 664 | $options['direction'] = is_rtl() ? 'rtl' : 'ltr'; |
| | 665 | |
| | 666 | // Set up the PAGING variable. |
| | 667 | if ( empty( $options['variables']['PAGING'] ) ) |
| | 668 | $options['variables']['PAGING'] = '%1$d'; |
| | 669 | if ( $paged >= 2 || $page >= 2 ) |
| | 670 | $options['variables']['PAGING'] = sprintf( $options['variables']['PAGING'], max( $paged, $page ) ); |
| | 671 | else |
| | 672 | $options['variables']['PAGING'] = ''; |
| | 673 | |
| | 674 | // Allow for a filter to set the title |
| | 675 | if ( null !== $title = apply_filters( 'title_tag_filter', null ) ) { |
| | 676 | if ( is_array($title) ) |
| | 677 | list( $title, $view ) = $title; |
| | 678 | else |
| | 679 | $view = 'filter'; |
| | 680 | // Standard home page view |
| | 681 | } elseif ( is_home() && is_front_page() ) { |
| | 682 | $title = get_bloginfo('name'); |
| | 683 | $view = 'home'; |
| | 684 | // Static Posts Page |
| | 685 | } elseif ( is_home() && !is_front_page() ) { |
| | 686 | $_post = get_post( get_option('page_for_posts') ); |
| | 687 | $title = $_post->post_title; |
| | 688 | $view = array( 'blog', 'page' ); |
| | 689 | // Static Front Page |
| | 690 | } elseif ( !is_home() && is_front_page() ) { |
| | 691 | $title = single_post_title( '', false ); |
| | 692 | $view = array( 'frontpage', 'page' ); |
| | 693 | // Post, Page, Attachment |
| | 694 | } else if ( is_singular() ) { |
| | 695 | $title = single_post_title( '', false ); |
| | 696 | $_post = get_queried_object(); |
| | 697 | $view = array( $_post->post_type . '-singular', 'singular' ); |
| | 698 | } |
| | 699 | // Category or Tag Archive |
| | 700 | elseif ( is_category() || is_tag() ) { |
| | 701 | $title = single_term_title( '', false ); |
| | 702 | $term = get_queried_object(); |
| | 703 | $view = array( $term->taxonomy . '-taxonomy-archive', 'taxonomy-archive', 'archive' ); |
| | 704 | } |
| | 705 | // Taxonomy Archive |
| | 706 | elseif ( is_tax() ) { |
| | 707 | $term = get_queried_object(); |
| | 708 | $tax = get_taxonomy( $term->taxonomy ); |
| | 709 | $title = single_term_title( $tax->labels->name, false ); |
| | 710 | $view = array( $term->taxonomy . '-taxonomy-archive', 'taxonomy-archive', 'archive' ); |
| | 711 | } |
| | 712 | // Author Archive |
| | 713 | elseif ( is_author() ) { |
| | 714 | $author = get_queried_object(); |
| | 715 | $title = $author->display_name; |
| | 716 | $view = array( 'author-archive', 'archive' ); |
| | 717 | } |
| | 718 | // Post Type Archive |
| | 719 | elseif ( is_post_type_archive() ) { |
| | 720 | $title = post_type_archive_title( '', false ); |
| | 721 | $_post = get_queried_object(); |
| | 722 | $view = array( $_post->query_var . '-post-type-archive', 'post-type-archive', 'archive' ); |
| | 723 | } |
| | 724 | // Date Archive |
| | 725 | elseif ( is_date() ) { |
| | 726 | if ( is_year() ) { |
| | 727 | if ( is_month() ) |
| | 728 | $title = is_day() ? get_the_date() : trim( single_month_title( ' ', false ) ); |
| | 729 | else |
| | 730 | $title = get_query_var( 'year' ); |
| | 731 | } elseif ( is_month() && !is_day() ) { |
| | 732 | $title = single_month_title( '', false ); |
| | 733 | } |
| | 734 | $view = array( 'date-archive', 'archive' ); |
| | 735 | } |
| | 736 | // Search Results |
| | 737 | elseif ( is_search() ) { |
| | 738 | $title = strip_tags( get_query_var('s') ); |
| | 739 | $view = 'search'; |
| | 740 | } |
| | 741 | // Page Not Found |
| | 742 | elseif ( is_404() ) { |
| | 743 | $view = '404'; |
| | 744 | } |
| | 745 | |
| | 746 | // If a view is set, loop through the views to find the relevant title and |
| | 747 | // title format. |
| | 748 | if ( !empty( $view ) ) { |
| | 749 | foreach ( (array) $view as $cur_view ) { |
| | 750 | if ( empty( $title_format ) && isset( $options["title_format-$cur_view"] ) ) |
| | 751 | $title_format = $options["title_format-$cur_view"]; |
| | 752 | if ( empty( $options_title ) && isset( $options["title-$cur_view"] ) ) |
| | 753 | $options_title = $options["title-$cur_view"]; |
| | 754 | } |
| | 755 | } |
| | 756 | |
| | 757 | // Set the default value if a match was not found. |
| | 758 | if ( empty( $title_format ) ) |
| | 759 | $title_format = $options['title_format']; |
| | 760 | if ( empty( $options_title ) ) |
| | 761 | $options_title = $options['title']; |
| | 762 | |
| | 763 | // If the title from the options contains a printf variable, replace it with |
| | 764 | // the title generated above and use the result as the new title. |
| | 765 | $use_options_title = false; |
| | 766 | if ( ! is_array( $options_title ) ) |
| | 767 | $options_title = array( $options_title ); |
| | 768 | foreach ( $options_title as $index => $options_title_part ) { |
| | 769 | if ( false !== strpos( $options_title_part, '%1$s' ) ) { |
| | 770 | $options_title[$index] = sprintf( $options_title_part, $title ); |
| | 771 | $use_options_title = true; |
| | 772 | } |
| | 773 | } |
| | 774 | if ( $use_options_title ) |
| | 775 | $title = $options_title; |
| | 776 | |
| | 777 | // Flip the title and title format if the direction is rtl. |
| | 778 | if ( 'rtl' == $options['direction'] ) { |
| | 779 | if ( is_array($title) ) |
| | 780 | $title = array_reverse( $title ); |
| | 781 | if ( is_array($title_format) ) |
| | 782 | $title_format = array_reverse( $title_format ); |
| | 783 | } |
| | 784 | |
| | 785 | // Loop through the title parts stored in the title format array. For each |
| | 786 | // title part, replace any found variables with their relevant value. If any |
| | 787 | // title part is empty, skip it to prevent duplicate seperators in the final |
| | 788 | // title. |
| | 789 | $new_title_format = array(); |
| | 790 | foreach ( (array) $title_format as $title_part ) { |
| | 791 | foreach ( $options['variables'] as $var => $val ) |
| | 792 | $title_part = str_replace( "%%$var%%", $val, $title_part ); |
| | 793 | if ( !empty( $title_part ) ) |
| | 794 | $new_title_format[] = $title_part; |
| | 795 | } |
| | 796 | |
| | 797 | // Merge the title format array with the seperator seperating each title |
| | 798 | // part. |
| | 799 | $title_format = implode( $options['sep'], $new_title_format ); |
| | 800 | |
| | 801 | // If the title is an array, merge the parts with the seperator seperating |
| | 802 | // each part. This allows for complex multi-part view-specific titles. |
| | 803 | if ( is_array($title) ) |
| | 804 | $title = implode( $options['sep'], $title ); |
| | 805 | |
| | 806 | // Construct the finished title by replacing the %%TITLE%% placeholder |
| | 807 | // variable with the view-specific title. |
| | 808 | $title = str_replace( '%%TITLE%%', $title, $title_format ); |
| | 809 | |
| | 810 | echo "<title>" . $title . "</title>\n"; |
| | 811 | } |
| | 812 | |
| | 813 | /** |