Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress Bootstrap 3 responsive images
<?php
//----------------------------------------------------------/
// responsive images [ 1) add img-responsive class 2) remove dimensions ]
//----------------------------------------------------------/
function bootstrap_responsive_images( $html ){
$classes = 'img-responsive'; // separated by spaces, e.g. 'img image-link'
// check if there are already classes assigned to the anchor
if ( preg_match('/<img.*? class="/', $html) ) {
$html = preg_replace('/(<img.*? class=".*?)(".*?\/>)/', '$1 ' . $classes . ' $2', $html);
} else {
$html = preg_replace('/(<img.*?)(\/>)/', '$1 class="' . $classes . '" $2', $html);
}
// remove dimensions from images,, does not need it!
$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
return $html;
}
add_filter( 'the_content','bootstrap_responsive_images',10 );
add_filter( 'post_thumbnail_html', 'bootstrap_responsive_images', 10 );
@MahbbRah

This comment has been minimized.

Copy link

MahbbRah commented Sep 15, 2015

Wow ! It's just great.. working ! Thank you dear.

@Floppy82

This comment has been minimized.

Copy link

Floppy82 commented Jan 21, 2016

really great! you helped me a lot!

@priyabratary

This comment has been minimized.

Copy link

priyabratary commented Mar 12, 2016

But it is removing the video width and height, can you please help ?

@martinkariuki7

This comment has been minimized.

Copy link

martinkariuki7 commented Apr 18, 2016

Beautiful!

@richyzak

This comment has been minimized.

Copy link

richyzak commented Aug 10, 2016

SUper et vraiment gentil

@kokushta

This comment has been minimized.

Copy link

kokushta commented Mar 24, 2017

Thanks! Very helpful!

@luisramirez-m

This comment has been minimized.

Copy link

luisramirez-m commented Jun 7, 2017

Thanks for this !, It worked perfect!

@keepsmilyn

This comment has been minimized.

Copy link

keepsmilyn commented Nov 23, 2018

This worked for me. But with one slight adjustment to make sure the photoswipe.min.js did no produce a JS error, this lines were commented out:

      `$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );`

Alternative version used as shown here, with some tweaks as required:
woocommerce/woocommerce#15376

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.