Filter hook 'wp_calculate_image_srcset'

in WP Core File wp-includes/media.php at line 1517

View Source

wp_calculate_image_srcset

Filter Hook
Description
Filters an image's 'srcset' sources. either 'w' or 'x'. pixel density value if paired with an 'x' descriptor. } } }

Hook Information

File Location wp-includes/media.php View on GitHub
Hook Type Filter
Line Number 1517

Hook Parameters

Type Name Description
array $sources { One or more arrays of source data to include in the 'srcset'.
array $size_array { An array of requested width and height values.
string $image_src The 'src' of the image.
array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
int $attachment_id Image attachment ID or 0.

Usage Examples

Basic Usage
<?php
// Hook into wp_calculate_image_srcset
add_filter('wp_calculate_image_srcset', 'my_custom_filter', 10, 5);

function my_custom_filter($sources, $size_array, $image_src, $image_meta, $attachment_id) {
    // Your custom filtering logic here
    return $sources;
}

Source Code Context

wp-includes/media.php:1517 - How this hook is used in WordPress core
<?php
1512  	 * }
1513  	 * @param string $image_src     The 'src' of the image.
1514  	 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
1515  	 * @param int    $attachment_id Image attachment ID or 0.
1516  	 */
1517  	$sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
1518  
1519  	// Only return a 'srcset' value if there is more than one source.
1520  	if ( ! $src_matched || ! is_array( $sources ) || count( $sources ) < 2 ) {
1521  		return false;
1522  	}

PHP Documentation

<?php
/**
	 * Filters an image's 'srcset' sources.
	 *
	 * @since 4.4.0
	 *
	 * @param array  $sources {
	 *     One or more arrays of source data to include in the 'srcset'.
	 *
	 *     @type array $width {
	 *         @type string $url        The URL of an image source.
	 *         @type string $descriptor The descriptor type used in the image candidate string,
	 *                                  either 'w' or 'x'.
	 *         @type int    $value      The source width if paired with a 'w' descriptor, or a
	 *                                  pixel density value if paired with an 'x' descriptor.
	 *     }
	 * }
	 * @param array $size_array     {
	 *     An array of requested width and height values.
	 *
	 *     @type int $0 The width in pixels.
	 *     @type int $1 The height in pixels.
	 * }
	 * @param string $image_src     The 'src' of the image.
	 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
	 * @param int    $attachment_id Image attachment ID or 0.
	 */
Quick Info
  • Hook Type: Filter
  • Parameters: 5
  • File: wp-includes/media.php
Related Hooks

Related hooks will be displayed here in future updates.