Filter hook 'wp_img_tag_add_width_and_height_attr'

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

View Source

wp_img_tag_add_width_and_height_attr

Filter Hook
Description
Filters whether to add the missing `width` and `height` HTML attributes to the img tag. Default `true`. Returning anything else than `true` will not add the attributes.

Hook Information

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

Hook Parameters

Type Name Description
bool $value The filtered value, defaults to `true`.
string $image The HTML `img` tag where the attribute should be added.
string $context Additional context about how the function was called or where the img tag is.
int $attachment_id The image attachment ID.

Usage Examples

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

function my_custom_filter($value, $image, $context, $attachment_id) {
    // Your custom filtering logic here
    return $value;
}

Source Code Context

wp-includes/media.php:2266 - How this hook is used in WordPress core
<?php
2261  	 * @param bool   $value         The filtered value, defaults to `true`.
2262  	 * @param string $image         The HTML `img` tag where the attribute should be added.
2263  	 * @param string $context       Additional context about how the function was called or where the img tag is.
2264  	 * @param int    $attachment_id The image attachment ID.
2265  	 */
2266  	$add = apply_filters( 'wp_img_tag_add_width_and_height_attr', true, $image, $context, $attachment_id );
2267  
2268  	if ( true === $add ) {
2269  		$image_meta = wp_get_attachment_metadata( $attachment_id );
2270  		$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id );
2271  

PHP Documentation

<?php
/**
	 * Filters whether to add the missing `width` and `height` HTML attributes to the img tag. Default `true`.
	 *
	 * Returning anything else than `true` will not add the attributes.
	 *
	 * @since 5.5.0
	 *
	 * @param bool   $value         The filtered value, defaults to `true`.
	 * @param string $image         The HTML `img` tag where the attribute should be added.
	 * @param string $context       Additional context about how the function was called or where the img tag is.
	 * @param int    $attachment_id The image attachment ID.
	 */
Quick Info
  • Hook Type: Filter
  • Parameters: 4
  • File: wp-includes/media.php
Related Hooks

Related hooks will be displayed here in future updates.