WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#30525 closed enhancement (duplicate)

Add width and height parameters to 'wp_get_attachment_image_attributes' filter

Reported by: hereswhatidid Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1
Component: Media Keywords:
Focuses: template Cc:

Description

Currently, in order to display an attachment image without width and height attributes you must do something like this:

$image = wp_get_attachment_image_src( $attachment_id, 'my-image-size' );
$alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true );
echo '<img src="' . $image[0] . '" alt="' . $alt . '" />';

This feels very clumsy and it only works on a call by call basis. To remove it via filters requires a regex to manipulate the full IMG tag as a string. What this patch does is add 'width' and 'height' attributes to the $default_attr array within the wp_get_attachment_image function so that they can be directly modified via the 'wp_get_attachment_image_attributes' filter. This makes it very easy to remove or modify the width and height attributes without any regex knowledge.

Here's an example with the new version that would remove all width and height attributes to any attachment images:

function remove_image_dimensions( $attr ) {
	unset( $attr['width'] );
	unset( $attr['height'] );
	return $attr;
}

add_filter( 'wp_get_attachment_image_attributes', 'remove_image_dimensions', 10 );

Attachments (1)

mediaattributes.diff (1.2 KB) - added by hereswhatidid 5 years ago.

Download all attachments as: .zip

Change History (2)

#1 @SergeyBiryukov
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #14110.

Note: See TracTickets for help on using tickets.