Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I found this great solution: Stop wordpress from hardcoding img width and height attributes

But I was wondering if I could do it only in one specific case and not in all the site because it messes up other stuff. This would be the case:

<?php echo wp_get_attachment_image($attachment->ID, 'six', false, $default_attr); ?>

Removing only height but not width in all the site would also work.

share|improve this question
Can you explain why you want to do this? Understanding your need may open up other solutions. – Stephen Nov 1 '12 at 23:17

2 Answers 2

You can also get the data for width and hight of a image.

$src = wp_get_attachment_image_src( $attachment_id, 'full' );

the var_dump output like this:

  0 => string '' (length=63)
  1 => int 1632
  2 => int 1224
  3 => boolean false

The results mean:

$src[0] => url
$src[1] => width
$src[2] => height
$src[3] => icon
share|improve this answer

This worked for me.

function remove_width_and_height_attribute( $html ) {
   return preg_replace( '/(height|width)="\d*"\s/', "", $html );

and then change

wp_get_attachment_image_src( $attachment_id, 'full' );


remove_width_and_height_attribute(wp_get_attachment_image_src( $attachment_id, 'full' ));
share|improve this answer
Instead of removing the width and height, you could also make them more useful by making a proper style attribute for them. Even though that's not what the OP asked, it will make the page less "jumpy" if there are lots of images to load: return preg_replace('/ width="(\d*)" height="(\d*)" /', ' style="width:$1px;height:$2px;" ', $html); – Sphinxxx Jul 18 at 2:25
inline style is generally considered a bad idea. Both for maintenance sake and speed. – Sam Jul 18 at 7:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.