Remove width and height attributes from [gallery] output

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

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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;
}

Method 2

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;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x