Jetpack by WordPress.com
[resolved] wp_get_attachment_image_src changed? (5 posts)

  1. macgamer
    Posted 3 years ago #

    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)?


  2. Erick Hitter
    Code Wizard
    Plugin Contributor

    Posted 3 years ago #


    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!

  3. macgamer
    Posted 3 years ago #

    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));

  4. Erick Hitter
    Code Wizard
    Plugin Contributor

    Posted 3 years ago #

    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!

  5. macgamer
    Posted 3 years ago #

    perfect, thank you.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Jetpack by WordPress.com
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic