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

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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 'http://example.com/wp-content/uploads/2009/08/DSC00261.JPG' (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 '15 at 2:25
inline style is generally considered a bad idea. Both for maintenance sake and speed. – Sam Jul 18 '15 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.