Posted on

Jetpack Photon image_downsize() issue

Sometimes in WordPress code you have the need to get an attached image, for example in a featured slider. To do this we make use of the wp_get_attachment_image_src() function.

The reason we do this is to grab the image properties such as height etc instead of just the url in order to add a property for dynamic slide heights.

However, there is a problem with this when a user is using Jetpack and the Photon service. Jetpack uses a function called image_downsize() which does not return the required image object/array. It simply returns the url.

So in the example below, height would not normally be able to be returned with Jetpack active:

$featured_height_id = get_post_thumbnail_id();
$featured_height = wp_get_attachment_image_src( $featured_height_id, 'full' );
$featured_height = ' height: ' . $featured_height[ 2 ] . 'px;';

However, I’ve added 2 lines that fixes this -the add_filter and remove_filter for jetpack_photon_override_image_downsize action.

$featured_height_id = get_post_thumbnail_id();
add_filter( 'jetpack_photon_override_image_downsize', '__return_true' );
$featured_height = wp_get_attachment_image_src( $featured_height_id, 'full' );
remove_filter( 'jetpack_photon_override_image_downsize', '__return_true' );
$featured_height = ' height: ' . $featured_height[ 2 ] . 'px;';

And before you point out that this is not technically Jetpack’s fault – it only occurs when using Jetpack Photon. Deactivate the plugin and all is fine and dandy. image_downsize() is in WordPress core (see link below).

Related Github issue here -> https://github.com/Automattic/jetpack/issues/607

image_downsize() codex -> http://codex.wordpress.org/Function_Reference/image_downsize

Leave a Reply