PageRenderTime 40ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/news/library/functions/core-seo.php

https://bitbucket.org/lgorence/quickpress
PHP | 244 lines | 93 code | 54 blank | 97 comment | 21 complexity | 16c60a6bb7a3975bbae4bcf3ed40aeb0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * SEO and header functions. Not all things in this file are strictly for search engine optimization. Many
  4. * of the functions handle basic <meta> elements for the <head> area of the site. This file is a catchall file
  5. * for adding these types of things to themes.
  6. *
  7. * @package HybridCore
  8. * @subpackage Functions
  9. * @author Justin Tadlock <justin@justintadlock.com>
  10. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  11. * @link http://themehybrid.com/hybrid-core
  12. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  13. */
  14. /* Add <meta> elements to the <head> area. */
  15. add_action( 'wp_head', 'hybrid_meta_robots', 1 );
  16. add_action( 'wp_head', 'hybrid_meta_author', 1 );
  17. add_action( 'wp_head', 'hybrid_meta_copyright', 1 );
  18. add_action( 'wp_head', 'hybrid_meta_revised', 1 );
  19. add_action( 'wp_head', 'hybrid_meta_description', 1 );
  20. add_action( 'wp_head', 'hybrid_meta_keywords', 1 );
  21. /**
  22. * Sets the default meta robots setting. If private, don't send meta info to the header. Runs the
  23. * hybrid_meta_robots filter hook at the end.
  24. *
  25. * @since 0.2.3
  26. * @access private
  27. * @return void
  28. */
  29. function hybrid_meta_robots() {
  30. /* If the blog is set to private, don't show anything. */
  31. if ( !get_option( 'blog_public' ) )
  32. return;
  33. /* Create the HTML for the robots meta tag. */
  34. $robots = '<meta name="robots" content="index,follow" />' . "\n";
  35. echo apply_atomic( 'meta_robots', $robots );
  36. }
  37. /**
  38. * Generates the meta author. For singular posts, it uses the post author's display name. For user/author
  39. * archives, it uses the user's display name.
  40. *
  41. * @since 0.3.3
  42. * @access private
  43. * @return void
  44. */
  45. function hybrid_meta_author() {
  46. /* Set an empty $author variable. */
  47. $author = '';
  48. /* Get the queried object. */
  49. $object = get_queried_object();
  50. /* If viewing a singular post, get the post author's display name. */
  51. if ( is_singular() )
  52. $author = get_the_author_meta( 'display_name', $object->post_author );
  53. /* If viewing a user/author archive, get the user's display name. */
  54. elseif ( is_author() )
  55. $author = get_the_author_meta( 'display_name', get_queried_object_id() );
  56. /* If an author was found, wrap it in the proper HTML and escape the author name. */
  57. if ( !empty( $author ) )
  58. $author = '<meta name="author" content="' . esc_attr( $author ) . '" />' . "\n";
  59. echo apply_atomic( 'meta_author', $author );
  60. }
  61. /**
  62. * Add the meta tag for copyright information to the header. Singular posts display the date the post was
  63. * published. All other pages will show the current year.
  64. *
  65. * @since 0.4.0
  66. * @access private
  67. * @return void
  68. */
  69. function hybrid_meta_copyright() {
  70. /* If viewing a singular post, get the post month and year. */
  71. if ( is_singular() )
  72. $date = get_the_time( esc_attr__( 'F Y', 'hybrid-core' ) );
  73. /* For all other views, get the current year. */
  74. else
  75. $date = date( esc_attr__( 'Y', 'hybrid-core' ) );
  76. /* Create the HTML for the copyright meta tag. */
  77. $copyright = '<meta name="copyright" content="' . sprintf( esc_attr__( 'Copyright (c) %1$s', 'hybrid-core' ), $date ) . '" />' . "\n";
  78. echo apply_atomic( 'meta_copyright', $copyright );
  79. }
  80. /**
  81. * Add the revised meta tag on the singular view of posts. This shows the last time the post was modified.
  82. *
  83. * @since 0.4.0
  84. * @access private
  85. * @return void
  86. */
  87. function hybrid_meta_revised() {
  88. /* Create an empty $revised variable. */
  89. $revised = '';
  90. /* If viewing a singular post, get the last modified date/time to use in the revised meta tag. */
  91. if ( is_singular() )
  92. $revised = '<meta name="revised" content="' . get_the_modified_time( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid-core' ) ) . '" />' . "\n";
  93. echo apply_atomic( 'meta_revised', $revised );
  94. }
  95. /**
  96. * Generates the meta description based on either metadata or the description for the object.
  97. *
  98. * @since 0.2.3
  99. * @access private
  100. * @return void
  101. */
  102. function hybrid_meta_description() {
  103. /* Set an empty $description variable. */
  104. $description = '';
  105. /* If viewing the home/posts page, get the site's description. */
  106. if ( is_home() ) {
  107. $description = get_bloginfo( 'description' );
  108. }
  109. /* If viewing a singular post. */
  110. elseif ( is_singular() ) {
  111. /* Get the meta value for the 'Description' meta key. */
  112. $description = get_post_meta( get_queried_object_id(), 'Description', true );
  113. /* If no description was found and viewing the site's front page, use the site's description. */
  114. if ( empty( $description ) && is_front_page() )
  115. $description = get_bloginfo( 'description' );
  116. /* For all other singular views, get the post excerpt. */
  117. elseif ( empty( $description ) )
  118. $description = get_post_field( 'post_excerpt', get_queried_object_id() );
  119. }
  120. /* If viewing an archive page. */
  121. elseif ( is_archive() ) {
  122. /* If viewing a user/author archive. */
  123. if ( is_author() ) {
  124. /* Get the meta value for the 'Description' user meta key. */
  125. $description = get_user_meta( get_query_var( 'author' ), 'Description', true );
  126. /* If no description was found, get the user's description (biographical info). */
  127. if ( empty( $description ) )
  128. $description = get_the_author_meta( 'description', get_query_var( 'author' ) );
  129. }
  130. /* If viewing a taxonomy term archive, get the term's description. */
  131. elseif ( is_category() || is_tag() || is_tax() )
  132. $description = term_description( '', get_query_var( 'taxonomy' ) );
  133. /* If viewing a custom post type archive. */
  134. elseif ( is_post_type_archive() ) {
  135. /* Get the post type object. */
  136. $post_type = get_post_type_object( get_query_var( 'post_type' ) );
  137. /* If a description was set for the post type, use it. */
  138. if ( isset( $post_type->description ) )
  139. $description = $post_type->description;
  140. }
  141. }
  142. /* Format the meta description. */
  143. if ( !empty( $description ) )
  144. $description = '<meta name="description" content="' . str_replace( array( "\r", "\n", "\t" ), '', esc_attr( strip_tags( $description ) ) ) . '" />' . "\n";
  145. echo apply_atomic( 'meta_description', $description );
  146. }
  147. /**
  148. * Generates meta keywords/tags for the site.
  149. *
  150. * @since 0.2.3
  151. * @access private
  152. * @return void
  153. */
  154. function hybrid_meta_keywords() {
  155. /* Set an empty $keywords variable. */
  156. $keywords = '';
  157. /* If on a singular post and not a preview. */
  158. if ( is_singular() && !is_preview() ) {
  159. /* Get the queried post. */
  160. $post = get_queried_object();
  161. /* Get the meta value for the 'Keywords' meta key. */
  162. $keywords = get_post_meta( get_queried_object_id(), 'Keywords', true );
  163. /* If no keywords were found. */
  164. if ( empty( $keywords ) ) {
  165. /* Get all taxonomies for the current post type. */
  166. $taxonomies = get_object_taxonomies( $post->post_type );
  167. /* If taxonomies were found for the post type. */
  168. if ( is_array( $taxonomies ) ) {
  169. /* Loop through the taxonomies, getting the terms for the current post. */
  170. foreach ( $taxonomies as $tax ) {
  171. if ( $terms = get_the_term_list( get_queried_object_id(), $tax, '', ', ', '' ) )
  172. $keywords[] = $terms;
  173. }
  174. /* If keywords were found, join the array into a comma-separated string. */
  175. if ( !empty( $keywords ) )
  176. $keywords = join( ', ', $keywords );
  177. }
  178. }
  179. }
  180. /* If on a user/author archive page, check for user meta. */
  181. elseif ( is_author() ) {
  182. /* Get the meta value for the 'Keywords' user meta key. */
  183. $keywords = get_user_meta( get_query_var( 'author' ), 'Keywords', true );
  184. }
  185. /* If we have keywords, format for output. */
  186. if ( !empty( $keywords ) )
  187. $keywords = '<meta name="keywords" content="' . esc_attr( strip_tags( $keywords ) ) . '" />' . "\n";
  188. echo apply_atomic( 'meta_keywords', $keywords );
  189. }
  190. ?>