WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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

WordPress automatically adds the width and height attributes to [gallery] shortcode images.

How to delete these attributes? Something like this but for [gallery] output.

share|improve this question
up vote 0 down vote accepted

I can think of a couple of options:

  1. Create a new shortcode, e.g. my_gallery. You can copy the code in wp-includes/media.php. Look for the code staring with add_shortcode('gallery', 'gallery_shortcode'); and then the actual function. Rename / modify as needed. The actual img link is within the $link variable

  2. Use something like this (untested), which should work but might strip-out all width/height from any link.


add_filter('wp_get_attachment_link', 'remove_img_width_height', 10, 1);

function remove_img_width_height($html) {
    $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
    return $html;
share|improve this answer
@TohidGolkar has the right idea below. Both of the options provided are terrible. Rewriting the core (1st solution), and removing widths/heights across all attachment links is no better. – Dan Jan 29 '13 at 6:33
I wasn't suggesting to rewrite the core, but just to adapt it (copy the code and re-use it as you see fit) and create your own version of it. As for the 2nd option - yeah, it's not great and I explained how it might alter all image links. I totally agree that the version of @TohidGolkar looks much better since it's more fine-grained. – Yoav Aner Jan 29 '13 at 10:22

if you set image size in function.php as a "gallery"

add_image_size( 'gallery', 200, 120, true );

you can remove width and height of specific image size such as "gallery":

add_filter('wp_get_attachment_link', 'remove_img_width_height', 10, 4);

function remove_img_width_height( $html, $post_id, $post_image_id,$post_thumbnail) {
    if ($post_thumbnail=='gallery'){
    $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
    return $html;
share|improve this answer
looks much better than my 2nd snippet / suggestion. +1 – Yoav Aner Jan 29 '13 at 10:25

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.