Support » Plugin: Jetpack by » wp_get_attachment_image_src changed?

  • Resolved macgamer


    it seems that the behaviour of wp_get_attachment_image_src has now changed to always return a photon url with the width string already appended.

    That being the case, how do I force it to use “resize” instead of the default “fit” (other than just doing a str_replace)?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Erick Hitter



    Photon uses either fit or resize depending on whether or not a registered image size requests cropping; the former is used if no cropping is desired, whereas the latter implements cropping.

    If you wish to override the default behaviour, there are two filters available in Photon’s image_downsize handler: jetpack_photon_image_downsize_string [ref] and jetpack_photon_image_downsize_array [ref]. If you are only dealing with registered image sizes (passing a string as the size parameter), the first filter will let you make the changes you need; the latter filter is necessary only if the image size is specified as an array.

    Hope this helps!

    Hmm, I’m sorry I’m not all that familiar with how filters work, so some of that went over my head.

    Here’s my current code for retrieving the image, what do I need to add to override the “fit” behaviour in this case (and only in this case)?

    $image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), array(365,170));

    Plugin Contributor Erick Hitter


    There are two aspects to using the filters I mentioned.

    First, you need to create a function that will make the necessary changes. For example, you could add the following to your theme’s functions.php file:

    function macgamer_jetpack_photon_image_downsize_array( $photon_args ) {
    	$photon_args['resize'] = $photon_args['fit'];
    	unset( $photon_args['fit'] );
    	return $photon_args;

    Then, you need to tie that function to the filter I mentioned previously. Since you only want it to apply to a specific request, you would modify that request to be:

    add_filter( 'jetpack_photon_image_downsize_array', 'macgamer_jetpack_photon_image_downsize_array' );
    $image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), array(365,170) );
    remove_filter( 'jetpack_photon_image_downsize_array', 'macgamer_jetpack_photon_image_downsize_array' );

    Hope this helps!

    perfect, thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp_get_attachment_image_src changed?’ is closed to new replies.