Here is a code snippet to add a default image to Jetpack if no image present in our post and we wish that image to be used by Jetpack when it publicizes our post.
When we publish a new post, Jetpack by WordPress automatically shares the post on social networks if we have enabled sharing module provided by it.
Jetpack crawls the post and looks for images that can be used when sharing that post on Facebook, on Twitter. It starts by looking for a featured image. If that is not define then slideshows and galleries are looked and at last for single images we may have inserted in our post. Even Jetpack can use an image that is hosted on another site and inserted to post.
You can add this code snippet to your theme’s functions.php file, if you may not have added any image to your post. In this way, your readers will see a default image when sharing that post on Facebook or Twitter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function ft_jetpack_custom_image( $media, $post_id, $args ) { if ( $media ) { return $media; } else { $permalink = get_permalink( $post_id ); $image_url = 'YOUR_LOGO_IMG_URL_HERE'; $url = apply_filters( 'jetpack_photon_url', $image_url ); return array( array( 'type' => 'image', 'from' => 'custom_fallback', 'src' => esc_url( $url ), 'href' => $permalink, ) ); } } add_filter( 'jetpack_images_get_images', 'ft_jetpack_custom_image', 10, 3 ); |
Replace ‘YOUR_LOGO_IMG_URL_HERE’ with your actual image url to add a default image to Jetpack. Please be aware that your image should be larger than 200 x 200px as per Facebook requirement otherwise your image will be ignored.