Changeset 9733 for sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/head.php
- Timestamp:
- 04/15/2020 10:12:33 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/head.php
r9715 r9733 22 22 */ 23 23 public static function do_init() { 24 add_action( 'wp_head', array( __CLASS__, 'output_head_tags' ), 2 ); 24 add_filter( 'document_title_parts', array( __CLASS__, 'document_title' ) ); 25 add_filter( 'document_title_separator', array( __CLASS__, 'document_title_separator' ) ); 26 add_action( 'wp_head', array( __CLASS__, 'rel_canonical' ), 9 ); 27 add_action( 'wp_head', array( __CLASS__, 'output_head_tags' ), 2 ); 28 } 29 30 /** 31 * Filters document title to add context based on what is being viewed. 32 * 33 * @param array $parts The document title parts. 34 * @return array The document title parts. 35 */ 36 public static function document_title( $parts ) { 37 global $page, $paged; 38 39 if ( is_feed() ) { 40 return $parts; 41 } 42 43 $title = $parts['title']; 44 $sep = '|'; 45 46 $post_type = get_query_var( 'post_type' ); 47 48 // Omit 'Home' from the home page. 49 if ( 'Home' === $title ) { 50 $title = ''; 51 } 52 // Add post type to title if it's a parsed item. 53 elseif ( is_singular() && \DevHub\is_parsed_post_type( $post_type ) ) { 54 if ( $post_type_object = get_post_type_object( $post_type ) ) { 55 $title .= " $sep " . get_post_type_object( $post_type )->labels->singular_name; 56 } 57 } 58 // Add handbook name to title if relevent 59 elseif ( ( is_singular() || is_post_type_archive() ) && false !== strpos( $post_type, 'handbook' ) ) { 60 if ( $post_type_object = get_post_type_object( $post_type ) ) { 61 $handbook_label = get_post_type_object( $post_type )->labels->name; 62 $handbook_name = \WPorg_Handbook::get_name( $post_type ) . " Handbook"; 63 64 // Replace title with handbook name if this is landing page for the handbook 65 if ( $title == $handbook_label ) { 66 $title = $handbook_name; 67 // Otherwise, append the handbook name 68 } else { 69 $title .= " $sep " . $handbook_name; 70 } 71 } 72 } 73 74 // Add a page number if necessary: 75 if ( isset( $parts['page'] ) && $parts['page'] >= 2 ) { 76 $title .= " $sep " . sprintf( __( 'Page %s', 'wporg' ), $parts['page'] ); 77 } 78 79 $parts['title'] = $title; 80 81 return $parts; 82 } 83 84 /** 85 * Customizes the document title separator. 86 * 87 * @param string $separator Current document title separator. 88 * @return string 89 */ 90 public static function document_title_separator( $separator ) { 91 return '|'; 25 92 } 26 93 … … 108 175 } 109 176 177 /** 178 * Outputs `<link rel="canonical">` tags where appropriate. 179 */ 180 public static function rel_canonical() { 181 $canonical = false; 182 $queried_object = get_queried_object(); 183 184 if ( is_tax() || is_tag() || is_category() ) { 185 $canonical = get_term_link( $queried_object ); 186 } elseif ( is_post_type_archive() ) { 187 $canonical = get_post_type_archive_link( $queried_object->name ); 188 } 189 190 if ( $canonical && get_query_var( 'paged' ) > 1 ) { 191 $canonical .= 'page/' . (int) get_query_var( 'paged' ) . '/'; 192 } 193 194 if ( $canonical ) { 195 printf( '<link rel="canonical" href="%s">' . "\n", esc_url( $canonical ) ); 196 } 197 } 198 110 199 } // DevHub_Head 111 200
Note: See TracChangeset
for help on using the changeset viewer.