Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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 );
- Wordpress Bootstrap 3 responsive images
- since you need to have it for all of your post images, then you need to add a hook for the content and add
- function add_responsive_class($content){
- $content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
- $document = new DOMDocument();
- libxml_use_internal_errors(true);
- $document->loadHTML(utf8_decode($content));
- $imgs = $document->getElementsByTagName('img');
- foreach ($imgs as $img) {
- $img->setAttribute('class','img-responsive');
- }
- $html = $document->saveHTML();
- return $html;
- }
- now add the hook to the content
- add_filter ('the_content', 'add_responsive_class');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement