Feb
23
2014

Code Snippet to Serve Default Thumbnail With Jetpack

Update: The image I assigned as the default one in the Default Featured Image plugin wasn’t big enough. After looking through the code I noticed Jetpack requires the image to be at least 200 x 200. This is surely documented somewhere but I missed it. You can always test your posts by plugging the URL in to the Facebook Debugger.

The Default Featured Image plugin either isn’t working correctly or the Jetpack plugin is interfering with it. The link below contains a filter that can be added to a theme to give Jetpack a default image to work with.

Help: can I specify a default/fallback image?

The relevant comment says:

There are different ways to work around this issue:

  • You could add your logo as a Featured Image on those posts where you didn’t insert any image.
  • You could use the jetpack_images_get_images filter to add your own fallback logo when Jetpack cannot find an image to use in the post. Here is how to do it:

 


function jeherve_custom_image( $media, $post_id, $args ) {
    if ( empty( $media ) ) {
        $permalink = get_permalink( $post_id );
        $url = apply_filters( 'jetpack_photon_url', 'YOUR_LOGO_IMG_URL' );

        return array( array(
            'type'  => 'image',
            'from'  => 'custom_fallback',
            'src'   => esc_url( $url ),
            'href'  => $permalink,
        ) );
    }
}
add_filter( 'jetpack_images_get_images', 'jeherve_custom_image', 10, 3 );

Leave a comment