[wp-trac] [WordPress Trac] #30525: Add width and height parameters to 'wp_get_attachment_image_attributes' filter
WordPress Trac
noreply at wordpress.org
Thu Nov 27 05:18:29 UTC 2014
#30525: Add width and height parameters to 'wp_get_attachment_image_attributes'
filter
---------------------------+-----------------------------
Reporter: hereswhatidid | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: trunk
Severity: normal | Keywords:
Focuses: template |
---------------------------+-----------------------------
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 );
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/30525>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list