wp_get_document_title

Returns document title for the current page.

Description

(string) wp_get_document_title(); 

Returns (string)

Tag with the document title.


Usage

  1. if ( !function_exists( 'wp_get_document_title' ) ) { 
  2. require_once ABSPATH . WPINC . '/general-template.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_get_document_title(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/general-template.php  
  1. function wp_get_document_title() { 
  2.  
  3. /** 
  4. * Filters the document title before it is generated. 
  5. * Passing a non-empty value will short-circuit wp_get_document_title(),  
  6. * returning that value instead. 
  7. * @since 4.4.0 
  8. * @param string $title The document title. Default empty string. 
  9. */ 
  10. if ( ! empty( $title ) ) { 
  11. return $title; 
  12.  
  13. global $page, $paged; 
  14.  
  15. $title = array( 
  16. 'title' => '',  
  17. ); 
  18.  
  19. // If it's a 404 page, use a "Page not found" title. 
  20. if ( is_404() ) { 
  21. $title['title'] = __( 'Page not found' ); 
  22.  
  23. // If it's a search, use a dynamic search results title. 
  24. } elseif ( is_search() ) { 
  25. /** translators: %s: search phrase */ 
  26. $title['title'] = sprintf( __( 'Search Results for “%s”' ), get_search_query() ); 
  27.  
  28. // If on the front page, use the site title. 
  29. } elseif ( is_front_page() ) { 
  30. $title['title'] = get_bloginfo( 'name', 'display' ); 
  31.  
  32. // If on a post type archive, use the post type archive title. 
  33. } elseif ( is_post_type_archive() ) { 
  34. $title['title'] = post_type_archive_title( '', false ); 
  35.  
  36. // If on a taxonomy archive, use the term title. 
  37. } elseif ( is_tax() ) { 
  38. $title['title'] = single_term_title( '', false ); 
  39.  
  40. /** 
  41. * If we're on the blog page that is not the homepage or 
  42. * a single post of any post type, use the post title. 
  43. */ 
  44. } elseif ( is_home() || is_singular() ) { 
  45. $title['title'] = single_post_title( '', false ); 
  46.  
  47. // If on a category or tag archive, use the term title. 
  48. } elseif ( is_category() || is_tag() ) { 
  49. $title['title'] = single_term_title( '', false ); 
  50.  
  51. // If on an author archive, use the author's display name. 
  52. } elseif ( is_author() && $author = get_queried_object() ) { 
  53. $title['title'] = $author->display_name; 
  54.  
  55. // If it's a date archive, use the date as the title. 
  56. } elseif ( is_year() ) { 
  57. $title['title'] = get_the_date( _x( 'Y', 'yearly archives date format' ) ); 
  58.  
  59. } elseif ( is_month() ) { 
  60. $title['title'] = get_the_date( _x( 'F Y', 'monthly archives date format' ) ); 
  61.  
  62. } elseif ( is_day() ) { 
  63. $title['title'] = get_the_date(); 
  64.  
  65. // Add a page number if necessary. 
  66. if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) { 
  67. $title['page'] = sprintf( __( 'Page %s' ), max( $paged, $page ) ); 
  68.  
  69. // Append the description or site title to give context. 
  70. if ( is_front_page() ) { 
  71. $title['tagline'] = get_bloginfo( 'description', 'display' ); 
  72. } else { 
  73. $title['site'] = get_bloginfo( 'name', 'display' ); 
  74.  
  75. /** 
  76. * Filters the separator for the document title. 
  77. * @since 4.4.0 
  78. * @param string $sep Document title separator. Default '-'. 
  79. */ 
  80.  
  81. /** 
  82. * Filters the parts of the document title. 
  83. * @since 4.4.0 
  84. * @param array $title { 
  85. * The document title parts. 
  86. * @type string $title Title of the viewed page. 
  87. * @type string $page Optional. Page number if paginated. 
  88. * @type string $tagline Optional. Site description when on home page. 
  89. * @type string $site Optional. Site title when not on home page. 
  90. * } 
  91. */ 
  92. $title = apply_filters( 'document_title_parts', $title ); 
  93.  
  94. $title = implode( " $sep ", array_filter( $title ) ); 
  95. $title = wptexturize( $title ); 
  96. $title = convert_chars( $title ); 
  97. $title = esc_html( $title ); 
  98. $title = capital_P_dangit( $title ); 
  99.  
  100. return $title;