Jetpack_SEO

An SEO expert walks into a bar, bars, pub, public house, Irish pub, drinks, beer, wine, liquor, Grey Goose, Cristal.

Defined (1)

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

/modules/seo-tools/jetpack-seo.php  
  1. class Jetpack_SEO { 
  2. public function __construct() { 
  3. add_action( 'init', array( $this, 'init' ) ); 
  4.  
  5. public function init() { 
  6. /** 
  7. * Can be used to prevent SEO tools from inserting custom meta tags. 
  8. * @module seo-tools 
  9. * @since 4.4.0 
  10. * @param bool true Should Jetpack's SEO Meta Tags be enabled. Defaults to true. 
  11. */ 
  12. if ( apply_filters( 'jetpack_seo_meta_tags_enabled', true ) ) { 
  13. add_action( 'wp_head', array( $this, 'meta_tags' ) ); 
  14.  
  15. // Add support for editing page excerpts in pages, regardless of theme support. 
  16. add_post_type_support( 'page', 'excerpt' ); 
  17.  
  18. /** 
  19. * Can be used to prevent SEO tools form modifying site titles. 
  20. * @module seo-tools 
  21. * @since 4.4.0 
  22. * @param bool true Should Jetpack SEO modify site titles. Defaults to true. 
  23. */ 
  24. if ( apply_filters( 'jetpack_seo_custom_titles', true ) ) { 
  25. // Overwrite page title with custom SEO meta title for themes that support title-tag. 
  26. add_filter( 'pre_get_document_title', array( 'Jetpack_SEO_Titles', 'get_custom_title' ) ); 
  27.  
  28. // Add overwrite support for themes that don't support title-tag. 
  29. add_filter( 'wp_title', array( 'Jetpack_SEO_Titles', 'get_custom_title' ) ); 
  30.  
  31. add_filter( 'jetpack_open_graph_tags', array( $this, 'set_custom_og_tags' ) ); 
  32.  
  33. private function get_authors() { 
  34. global $wp_query; 
  35.  
  36. $authors = array(); 
  37.  
  38. foreach ( $wp_query->posts as $post ) { 
  39. $authors[] = get_the_author_meta( 'display_name', (int) $post->post_author ); 
  40.  
  41. $authors = array_unique( $authors ); 
  42.  
  43. return $authors; 
  44.  
  45. public function set_custom_og_tags( $tags ) { 
  46. $custom_title = Jetpack_SEO_Titles::get_custom_title(); 
  47.  
  48. if ( ! empty( $custom_title ) ) { 
  49. $tags['og:title'] = $custom_title; 
  50.  
  51. $post_custom_description = Jetpack_SEO_Posts::get_post_custom_description( get_post() ); 
  52. $front_page_meta = Jetpack_SEO_Utils::get_front_page_meta_description(); 
  53.  
  54. if ( is_front_page() && ! empty( $front_page_meta ) ) { 
  55. $tags['og:description'] = $front_page_meta; 
  56. } else { 
  57. if ( ! empty( $post_custom_description ) ) { 
  58. $tags['og:description'] = $post_custom_description; 
  59.  
  60. return $tags; 
  61.  
  62. public function meta_tags() { 
  63. global $wp_query; 
  64.  
  65. $period = ''; 
  66. $template = ''; 
  67. $meta = array(); 
  68.  
  69. /** 
  70. * Can be used to specify a list of themes that set their own meta tags. 
  71. * If current site is using one of the themes listed as conflicting, inserting Jetpack SEO 
  72. * meta tags will be prevented. 
  73. * @module seo-tools 
  74. * @since 4.4.0 
  75. * @param array List of conflicted theme names. Defaults to empty array. 
  76. */ 
  77. $conflicted_themes = apply_filters( 'jetpack_seo_meta_tags_conflicted_themes', array() ); 
  78.  
  79. if ( isset( $conflicted_themes[ get_option( 'template' ) ] ) ) { 
  80. return; 
  81.  
  82. /** 
  83. * Can be used to insert custom site host that will used for meta title. 
  84. * @module seo-tools 
  85. * @since 4.4.0 
  86. * @param string Name of the site host. Defaults to empty string. 
  87. */ 
  88. $site_host = apply_filters( 'jetpack_seo_site_host', '' ); 
  89.  
  90. $meta['title'] = sprintf( _x( '%1$s', 'Site Title', 'jetpack' ), get_bloginfo( 'title' ) ); 
  91.  
  92. if ( ! empty( $site_host ) ) { 
  93. $meta['title'] = sprintf( 
  94. _x( '%1$s on %2$s', 'Site Title on WordPress', 'jetpack' ),  
  95. get_bloginfo( 'title' ),  
  96. $site_host 
  97. ); 
  98.  
  99. $front_page_meta = Jetpack_SEO_Utils::get_front_page_meta_description(); 
  100. $description = $front_page_meta ? $front_page_meta : get_bloginfo( 'description' ); 
  101. $meta['description'] = trim( $description ); 
  102.  
  103. // Try to target things if we're on a "specific" page of any kind. 
  104. if ( is_singular() ) { 
  105. $meta['title'] = sprintf( 
  106. _x( '%1$s | %2$s', 'Post Title | Site Title on WordPress', 'jetpack' ),  
  107. get_the_title(),  
  108. $meta['title'] 
  109. ); 
  110.  
  111. // Business users can overwrite the description. 
  112. if ( ! ( is_front_page() && Jetpack_SEO_Utils::get_front_page_meta_description() ) ) { 
  113. $description = Jetpack_SEO_Posts::get_post_description( get_post() ); 
  114.  
  115. if ( $description ) { 
  116. $description = wp_trim_words( strip_shortcodes( wp_kses( $description, array() ) ) ); 
  117. $meta['description'] = $description; 
  118.  
  119. } elseif ( is_author() ) { 
  120. $obj = get_queried_object(); 
  121.  
  122. $meta['title'] = sprintf( 
  123. _x( 'Posts by %1$s | %2$s', 'Posts by Author Name | Blog Title on WordPress', 'jetpack' ),  
  124. $obj->display_name,  
  125. $meta['title'] 
  126. ); 
  127.  
  128. $meta['description'] = sprintf( 
  129. _x( 'Read all of the posts by %1$s on %2$s', 'Read all of the posts by Author Name on Blog Title', 'jetpack' ),  
  130. $obj->display_name,  
  131. get_bloginfo( 'title' ) 
  132. ); 
  133. } elseif ( is_tag() || is_category() || is_tax() ) { 
  134. $obj = get_queried_object(); 
  135.  
  136. $meta['title'] = sprintf( 
  137. _x( 'Posts about %1$s on %2$s', 'Posts about Category on Blog Title', 'jetpack' ),  
  138. single_term_title( '', false ),  
  139. get_bloginfo( 'title' ) 
  140. ); 
  141.  
  142. $description = get_term_field( 'description', $obj->term_id, $obj->taxonomy, 'raw' ); 
  143.  
  144. if ( ! is_wp_error( $description ) && '' != $description ) { 
  145. $meta['description'] = wp_trim_words( $description ); 
  146. } else { 
  147. $authors = $this->get_authors(); 
  148.  
  149. $meta['description'] = wp_sprintf( 
  150. _x( 'Posts about %1$s written by %2$l', 'Posts about Category written by John and Bob', 'jetpack' ),  
  151. single_term_title( '', false ),  
  152. $authors 
  153. ); 
  154. } elseif ( is_date() ) { 
  155. if ( is_year() ) { 
  156. $period = get_query_var( 'year' ); 
  157.  
  158. $template = _nx( 
  159. '%1$s post published by %2$l in the year %3$s', // singular 
  160. '%1$s posts published by %2$l in the year %3$s', // plural 
  161. count( $wp_query->posts ), // number 
  162. '10 posts published by John in the year 2012', // context 
  163. 'jetpack' 
  164. ); 
  165. } elseif ( is_month() ) { 
  166. $period = date( 'F Y', mktime( 0, 0, 0, get_query_var( 'monthnum' ), 1, get_query_var( 'year' ) ) ); 
  167.  
  168. $template = _nx( 
  169. '%1$s post published by %2$l during %3$s', // singular 
  170. '%1$s posts published by %2$l during %3$s', // plural 
  171. count( $wp_query->posts ), // number 
  172. '10 posts publishes by John during May 2012', // context 
  173. 'jetpack' 
  174. ); 
  175. } elseif ( is_day() ) { 
  176. $period = date( 
  177. 'F j, Y',  
  178. mktime( 0, 0, 0, get_query_var( 'monthnum' ), get_query_var( 'day' ), get_query_var( 'year' ) ) 
  179. ); 
  180.  
  181. $template = _nx( 
  182. '%1$s post published by %2$l on %3$s', // singular 
  183. '%1$s posts published by %2$l on %3$s', // plural 
  184. count( $wp_query->posts ), // number 
  185. '10 posts published by John on May 30, 2012', // context 
  186. 'jetpack' 
  187. ); 
  188.  
  189. $meta['title'] = sprintf( 
  190. _x( 'Posts from %1$s on %2$s', 'Posts from May 2012 on Blog Title', 'jetpack' ),  
  191. $period,  
  192. get_bloginfo( 'title' ) 
  193. ); 
  194.  
  195. $authors = $this->get_authors(); 
  196. $meta['description'] = wp_sprintf( $template, count( $wp_query->posts ), $authors, $period ); 
  197.  
  198. $custom_title = Jetpack_SEO_Titles::get_custom_title(); 
  199.  
  200. if ( ! empty( $custom_title ) ) { 
  201. $meta['title'] = $custom_title; 
  202.  
  203. /** 
  204. * Can be used to edit the default SEO tools meta tags. 
  205. * @module seo-tools 
  206. * @since 4.4.0 
  207. * @param array Array that consists of meta name and meta content pairs. 
  208. */ 
  209. $meta = apply_filters( 'jetpack_seo_meta_tags', $meta ); 
  210.  
  211. // Output them 
  212. foreach ( $meta as $name => $content ) { 
  213. if ( ! empty( $content ) ) { 
  214. echo '<meta name="' . esc_attr( $name ) . '" content="' . esc_attr( $content ) . '" />' . "\n";