WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#34757 closed defect (bug) (worksforme)

Add filter, probably image_downsize, for image urls in wp_calculate_image_srcset()

Reported by: klihelp Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.4
Component: Media Keywords:
Focuses: Cc:

Description

I am able to use the image_downsize filter to return custom images stored outside the wp-uploads directory. The wp_calculate_image_srcset() calculate the image sources to include in a 'srcset' attributes; images sources are not in sync with the image_downsize filter.

Change History (3)

#1 @joemcgill
3 years ago

Hi @klihelp,

wp_calculate_image_srcset() contains it's own filter, named 'wp_calculate_image_srcset', which can be used to modify the URLs of images included in the srcset attribute. The filter passes each source as an array containing the URL, the descriptor value (the image width in pixels), and a character representing the descriptor type (i.e., 'w').

#2 @klihelp
3 years ago

  • Resolution set to worksforme
  • Status changed from new to closed

Thanks!

// Change imges urls in scrset

function re_calculate_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) {
	
	if ( eazyest_folderbase()->is_gallery_image( $attachment_id ) ) {
		$uploads_url = wp_upload_dir();
		$uploads_url = trailingslashit( $uploads_url['baseurl'] );

		foreach ($sources as $key => $value) {
			$sources[$key]['url'] = str_replace($uploads_url, eazyest_gallery()->address(), $value['url'] );
		}
	}
	
	return $sources;
}
add_filter('wp_calculate_image_srcset', 're_calculate_image_srcset', 100, 5);


#3 @netweb
3 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.