jetpack_og_tags

The Jetpack by WordPress.com jetpack og tags function.

Description

jetpack_og_tags(); 

Usage

  1. if ( !function_exists( 'jetpack_og_tags' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/functions.opengraph.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = jetpack_og_tags(); 
  7.  

Defined (1)

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

/functions.opengraph.php  
  1. function jetpack_og_tags() { 
  2. /** 
  3. * Allow Jetpack to output Open Graph Meta Tags. 
  4. * @module sharedaddy, publicize 
  5. * @since 2.0.0 
  6. * @deprecated 2.0.3 Duplicative filter. Use `jetpack_enable_open_graph`. 
  7. * @param bool true Should Jetpack's Open Graph Meta Tags be enabled. Default to true. 
  8. */ 
  9. if ( false === apply_filters( 'jetpack_enable_opengraph', true ) ) { 
  10. _deprecated_function( 'jetpack_enable_opengraph', '2.0.3', 'jetpack_enable_open_graph' ); 
  11. return; 
  12.  
  13. // Disable the widont filter on WP.com to avoid stray  s 
  14. $disable_widont = remove_filter( 'the_title', 'widont' ); 
  15.  
  16. $og_output = "\n<!-- Jetpack Open Graph Tags -->\n"; 
  17. $tags = array(); 
  18.  
  19. /** 
  20. * Filter the minimum width of the images used in Jetpack Open Graph Meta Tags. 
  21. * @module sharedaddy, publicize 
  22. * @since 2.0.0 
  23. * @param int 200 Minimum image width used in Jetpack Open Graph Meta Tags. 
  24. */ 
  25. $image_width = absint( apply_filters( 'jetpack_open_graph_image_width', 200 ) ); 
  26. /** 
  27. * Filter the minimum height of the images used in Jetpack Open Graph Meta Tags. 
  28. * @module sharedaddy, publicize 
  29. * @since 2.0.0 
  30. * @param int 200 Minimum image height used in Jetpack Open Graph Meta Tags. 
  31. */ 
  32. $image_height = absint( apply_filters( 'jetpack_open_graph_image_height', 200 ) ); 
  33. $description_length = 197; 
  34.  
  35. if ( is_home() || is_front_page() ) { 
  36. $site_type = get_option( 'open_graph_protocol_site_type' ); 
  37. $tags['og:type'] = ! empty( $site_type ) ? $site_type : 'website'; 
  38. $tags['og:title'] = get_bloginfo( 'name' ); 
  39. $tags['og:description'] = get_bloginfo( 'description' ); 
  40.  
  41. $front_page_id = get_option( 'page_for_posts' ); 
  42. if ( $front_page_id && is_home() ) 
  43. $tags['og:url'] = get_permalink( $front_page_id ); 
  44. else 
  45. $tags['og:url'] = home_url( '/' ); 
  46.  
  47. // Associate a blog's root path with one or more Facebook accounts 
  48. $facebook_admins = get_option( 'facebook_admins' ); 
  49. if ( ! empty( $facebook_admins ) ) 
  50. $tags['fb:admins'] = $facebook_admins; 
  51.  
  52. } else if ( is_author() ) { 
  53. $tags['og:type'] = 'profile'; 
  54.  
  55. $author = get_queried_object(); 
  56.  
  57. $tags['og:title'] = $author->display_name; 
  58. if ( ! empty( $author->user_url ) ) { 
  59. $tags['og:url'] = $author->user_url; 
  60. } else { 
  61. $tags['og:url'] = get_author_posts_url( $author->ID ); 
  62. $tags['og:description'] = $author->description; 
  63. $tags['profile:first_name'] = get_the_author_meta( 'first_name', $author->ID ); 
  64. $tags['profile:last_name'] = get_the_author_meta( 'last_name', $author->ID ); 
  65.  
  66. } else if ( is_singular() ) { 
  67. global $post; 
  68. $data = $post; // so that we don't accidentally explode the global 
  69.  
  70. $tags['og:type'] = 'article'; 
  71. if ( empty( $data->post_title ) ) { 
  72. $tags['og:title'] = ' '; 
  73. } else { 
  74. /** This filter is documented in core/src/wp-includes/post-template.php */ 
  75. $tags['og:title'] = wp_kses( apply_filters( 'the_title', $data->post_title, $data->ID ), array() ); 
  76.  
  77. $tags['og:url'] = get_permalink( $data->ID ); 
  78. if ( ! post_password_required() ) { 
  79. if ( ! empty( $data->post_excerpt ) ) { 
  80. $tags['og:description'] = preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $data->post_excerpt, array() ) ) ); 
  81. } else { 
  82. $exploded_content_on_more_tag = explode( '<!--more-->', $data->post_content ); 
  83. $tags['og:description'] = wp_trim_words( preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $exploded_content_on_more_tag[0], array() ) ) ) ); 
  84. if ( empty( $tags['og:description'] ) ) { 
  85. $tags['og:description'] = __('Visit the post for more.', jetpack); 
  86. } else { 
  87. // Intentionally not using a filter to prevent pollution. @see https://github.com/Automattic/jetpack/pull/2899#issuecomment-151957382 
  88. $tags['og:description'] = wp_kses( trim( convert_chars( wptexturize( $tags['og:description'] ) ) ), array() ); 
  89.  
  90. $tags['article:published_time'] = date( 'c', strtotime( $data->post_date_gmt ) ); 
  91. $tags['article:modified_time'] = date( 'c', strtotime( $data->post_modified_gmt ) ); 
  92. if ( post_type_supports( get_post_type( $data ), 'author' ) && isset( $data->post_author ) ) { 
  93. $publicize_facebook_user = get_post_meta( $data->ID, '_publicize_facebook_user', true ); 
  94. if ( ! empty( $publicize_facebook_user ) ) { 
  95. $tags['article:author'] = esc_url( $publicize_facebook_user ); 
  96.  
  97. /** 
  98. * Allow plugins to inject additional template-specific Open Graph tags. 
  99. * @module sharedaddy, publicize 
  100. * @since 3.0.0 
  101. * @param array $tags Array of Open Graph Meta tags. 
  102. * @param array $args Array of image size parameters. 
  103. */ 
  104. $tags = apply_filters( 'jetpack_open_graph_base_tags', $tags, compact( 'image_width', 'image_height' ) ); 
  105.  
  106. // Re-enable widont if we had disabled it 
  107. if ( $disable_widont ) 
  108. add_filter( 'the_title', 'widont' ); 
  109.  
  110. /** 
  111. * Do not return any Open Graph Meta tags if we don't have any info about a post. 
  112. * @module sharedaddy, publicize 
  113. * @since 3.0.0 
  114. * @param bool true Do not return any Open Graph Meta tags if we don't have any info about a post. 
  115. */ 
  116. if ( empty( $tags ) && apply_filters( 'jetpack_open_graph_return_if_empty', true ) ) 
  117. return; 
  118.  
  119. $tags['og:site_name'] = get_bloginfo( 'name' ); 
  120.  
  121. // Get image info and build tags 
  122. if ( ! post_password_required() ) { 
  123. $image_info = jetpack_og_get_image( $image_width, $image_height ); 
  124. $tags['og:image'] = $image_info['src']; 
  125.  
  126. if ( ! empty( $image_info['width'] ) ) { 
  127. $tags['og:image:width'] = $image_info['width']; 
  128. if ( ! empty( $image_info['height'] ) ) { 
  129. $tags['og:image:height'] = $image_info['height']; 
  130.  
  131. // Facebook whines if you give it an empty title 
  132. if ( empty( $tags['og:title'] ) ) 
  133. $tags['og:title'] = __( '(no title)', jetpack ); 
  134.  
  135. // Shorten the description if it's too long 
  136. if ( isset( $tags['og:description'] ) ) { 
  137. $tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? mb_substr( $tags['og:description'], 0, $description_length ) . '*' : $tags['og:description']; 
  138.  
  139. // Try to add OG locale tag if the WP->FB data mapping exists 
  140. if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { 
  141. $_locale = get_locale(); 
  142.  
  143. // We have to account for WP.org vs WP.com locale divergence 
  144. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  145. $gp_locale = GP_Locales::by_field( 'slug', $_locale ); 
  146. } else { 
  147. $gp_locale = GP_Locales::by_field( wp_locale, $_locale ); 
  148.  
  149. if ( isset( $gp_locale->facebook_locale ) && ! empty( $gp_locale->facebook_locale ) ) { 
  150. $tags['og:locale'] = $gp_locale->facebook_locale; 
  151.  
  152. /** 
  153. * Allow the addition of additional Open Graph Meta tags, or modify the existing tags. 
  154. * @module sharedaddy, publicize 
  155. * @since 2.0.0 
  156. * @param array $tags Array of Open Graph Meta tags. 
  157. * @param array $args Array of image size parameters. 
  158. */ 
  159. $tags = apply_filters( 'jetpack_open_graph_tags', $tags, compact( 'image_width', 'image_height' ) ); 
  160.  
  161. // secure_urls need to go right after each og:image to work properly so we will abstract them here 
  162. $secure = $tags['og:image:secure_url'] = ( empty( $tags['og:image:secure_url'] ) ) ? '' : $tags['og:image:secure_url']; 
  163. unset( $tags['og:image:secure_url'] ); 
  164. $secure_image_num = 0; 
  165.  
  166. foreach ( (array) $tags as $tag_property => $tag_content ) { 
  167. // to accommodate multiple images 
  168. $tag_content = (array) $tag_content; 
  169. $tag_content = array_unique( $tag_content ); 
  170.  
  171. foreach ( $tag_content as $tag_content_single ) { 
  172. if ( empty( $tag_content_single ) ) 
  173. continue; // Don't ever output empty tags 
  174. $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) ); 
  175. /** 
  176. * Filter the HTML Output of each Open Graph Meta tag. 
  177. * @module sharedaddy, publicize 
  178. * @since 2.0.0 
  179. * @param string $og_tag HTML HTML Output of each Open Graph Meta tag. 
  180. */ 
  181. $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag ); 
  182. $og_output .= "\n"; 
  183.  
  184. if ( 'og:image' == $tag_property ) { 
  185. if ( is_array( $secure ) && !empty( $secure[$secure_image_num] ) ) { 
  186. $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure[ $secure_image_num ] ) ); 
  187. /** This filter is documented in functions.opengraph.php */ 
  188. $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag ); 
  189. $og_output .= "\n"; 
  190. } else if ( !is_array( $secure ) && !empty( $secure ) ) { 
  191. $og_tag = sprintf( '<meta property="og:image:secure_url" content="%s" />', esc_url( $secure ) ); 
  192. /** This filter is documented in functions.opengraph.php */ 
  193. $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag ); 
  194. $og_output .= "\n"; 
  195. $secure_image_num++; 
  196. echo $og_output;