jetpack_og_get_image

The Jetpack by WordPress.com jetpack og get image function.

Description

jetpack_og_get_image( (int) $width = 200, (int) $height = 200, (int) $max_images = 4 ); 

Parameters (3)

0. $width — Optional. (int) => 200
The width.
1. $height — Optional. (int) => 200
The height.
2. $max_images — Optional. (int) => 4
The max images.

Usage

  1. if ( !function_exists( 'jetpack_og_get_image' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/functions.opengraph.php'; 
  3.  
  4. // The width. 
  5. $width = 200; 
  6.  
  7. // The height. 
  8. $height = 200; 
  9.  
  10. // The max images. 
  11. $max_images = 4; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = jetpack_og_get_image($width, $height, $max_images); 
  15.  

Defined (1)

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

/functions.opengraph.php  
  1. function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { // Facebook requires thumbnails to be a minimum of 200x200 
  2. $image = ''; 
  3.  
  4. if ( is_singular() && ! is_home() ) { 
  5. global $post; 
  6. $image = ''; 
  7.  
  8. // Grab obvious image if $post is an attachment page for an image 
  9. if ( is_attachment( $post->ID ) && 'image' == substr( $post->post_mime_type, 0, 5 ) ) { 
  10. $image = wp_get_attachment_url( $post->ID ); 
  11.  
  12. // Attempt to find something good for this post using our generalized PostImages code 
  13. if ( ! $image && class_exists( 'Jetpack_PostImages' ) ) { 
  14. $post_images = Jetpack_PostImages::get_images( $post->ID, array( 'width' => $width, 'height' => $height ) ); 
  15. if ( $post_images && ! is_wp_error( $post_images ) ) { 
  16. $image = array(); 
  17. foreach ( (array) $post_images as $post_image ) { 
  18. $image['src'] = $post_image['src']; 
  19. if ( isset( $post_image['src_width'], $post_image['src_height'] ) ) { 
  20. $image['width'] = $post_image['src_width']; 
  21. $image['height'] = $post_image['src_height']; 
  22. } else if ( is_author() ) { 
  23. $author = get_queried_object(); 
  24. if ( function_exists( 'get_avatar_url' ) ) { 
  25. // Prefer the core function get_avatar_url() if available, WP 4.2+ 
  26. $image['src'] = get_avatar_url( $author->user_email, array( 'size' => $width ) ); 
  27. else { 
  28. $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' ); 
  29. if ( ! $has_filter ) { 
  30. add_filter( 'pre_option_show_avatars', '__return_true' ); 
  31. $avatar = get_avatar( $author->user_email, $width ); 
  32. if ( ! $has_filter ) { 
  33. remove_filter( 'pre_option_show_avatars', '__return_true' ); 
  34.  
  35. if ( ! empty( $avatar ) && ! is_wp_error( $avatar ) ) { 
  36. if ( preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) ); 
  37. $image['src'] = wp_specialchars_decode( $matches[1], ENT_QUOTES ); 
  38.  
  39. if ( empty( $image ) ) { 
  40. $image = array(); 
  41. } else if ( ! is_array( $image ) ) { 
  42. $image = array( 
  43. 'src' => $image 
  44. ); 
  45.  
  46. // First fall back, blavatar 
  47. if ( empty( $image ) && function_exists( 'blavatar_domain' ) ) { 
  48. $blavatar_domain = blavatar_domain( site_url() ); 
  49. if ( blavatar_exists( $blavatar_domain ) ) { 
  50. $image['src'] = blavatar_url( $blavatar_domain, 'img', $width, false, true ); 
  51. $image['width'] = $width; 
  52. $image['height'] = $height; 
  53.  
  54. // Second fall back, Site Logo 
  55. if ( empty( $image ) && ( function_exists( 'jetpack_has_site_logo' ) && jetpack_has_site_logo() ) ) { 
  56. $image['src'] = jetpack_get_site_logo( 'url' ); 
  57. $image_dimensions = jetpack_get_site_logo_dimensions(); 
  58. if ( ! empty( $image_dimensions ) ) { 
  59. $image['width'] = $image_dimensions['width']; 
  60. $image['height'] = $image_dimensions['height']; 
  61.  
  62. // Third fall back, Site Icon 
  63. if ( empty( $image ) && ( function_exists( 'jetpack_has_site_icon' ) && jetpack_has_site_icon() ) ) { 
  64. $image['src'] = jetpack_site_icon_url( null, '512' ); 
  65. $image['width'] = '512'; 
  66. $image['height'] = '512'; 
  67.  
  68. // Fourth fall back, Core Site Icon. Added in WP 4.3. 
  69. if ( empty( $image ) && ( function_exists( 'has_site_icon') && has_site_icon() ) ) { 
  70. $image['src'] = get_site_icon_url( null, '512' ); 
  71.  
  72. // Finally fall back, blank image 
  73. if ( empty( $image ) ) { 
  74. /** 
  75. * Filter the default Open Graph Image tag, used when no Image can be found in a post. 
  76. * @since 3.0.0 
  77. * @param string $str Default Image URL. 
  78. */ 
  79. $image['src'] = apply_filters( 'jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg' ); 
  80.  
  81. return $image;