apply_filters ( 'jetpack_images_get_images', array $media, int $post_id, array $args )

Filters the array of images that would be good for a specific post.<p>This filter is applied after options ($args) filter the original array.</p>

Source file: class.jetpack-post-images.php

View in GitHub

Parameters

$media

(array) Array of images that would be good for a specific post.

$post_id

(int) Post ID.

$args

(array) Array of options to get images.


Changelog

Since: Jetpack 2.0.0


Notes

You could use this filter to provide default images for Related Posts, or for any of the other Jetpack features picking images in your posts, like the Open Graph Meta Tags or the Top Posts module.

Here is how to use the filter:

function jeherve_custom_image( $media, $post_id, $args ) {
    if ( $media ) {
        return $media;
    } else {
        $permalink = get_permalink( $post_id );
        $url = apply_filters( 'jetpack_photon_url', 'YOUR_FALLBACK_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 Reply to Jeremy Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Comments

  1. Is it possible to use the theme’s site logo instead of a url with this snippet?

  2. Hello again. I have tried this, but I’m a complete N00b when it comes to code, so I would be extremely happy if you could give me a complete example on how to do this. I really have no idea what I’m doing. Thanks for any help!

    • Jeremy says:

      I haven’t tested it myself, but something like this should work:

      function jeherve_custom_image( $media, $post_id, $args ) {
          if ( $media ) {
              return $media;
          } else {
              $permalink = get_permalink( $post_id );
              $custom_logo_id = get_theme_mod( 'custom_logo' );
              $image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
              $url = apply_filters( 'jetpack_photon_url', $image[0] );
             
              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 );