How to add automatic class in image for wordpress post

I want to make a responsive theme with Bootstrap 3. However, I need to automatically add the CSS class .img-responsive to every post image because I need the images to be responsive.

Please suggest me what I need to add in WordPress’s functions.php file or any other file that will allow me to add the CSS class automatically.

Related posts

Leave a Reply

12 comments

  1. 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');
    

    However, if you already have classes for the img and you need to add a new class then you can refer to PHP equivalent to jQuery addClass. Or, you can simply do this:

    $existing_class = $img->getAttribute('class');
    $img->setAttribute('class', "img-responsive $existing_class");
    

    The code above works .. i use it to remove src and data-src for image lazy loading. Hope it works for you

  2. I think the easiest way is to use CSS like this.

    .content img { height: auto; max-width: 100%; }
    

    Where .content is the area that contains your post content.

    Note: You may also want to override the .wp-caption class as well like so.

    .wp-caption { width: auto !important; }
    
  3. I had the same question, and adding this function to functions.php worked for me.

    function add_image_responsive_class($content) {
       global $post;
       $pattern ="/<img(.*?)class="(.*?)"(.*?)>/i";
       $replacement = '<img$1class="$2 img-responsive"$3>';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;
    }
    add_filter('the_content', 'add_image_responsive_class');
    
  4. Not quite sure how good this answer is performance wise but it works. Just put this in functions.php.

    function img_responsive($content){
        return str_replace('<img class="','<img class="img-responsive ',$content);
    }
    add_filter('the_content','img_responsive');
    

    Please note that you need the space after class="img-responsive so it doesn’t merge with other classes.

  5. I think you don’t require to add class to make image responsive.
    just remove height width from featured image, image will become responsive definitely.

    There is code put in your function.php to remove height width

    add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10, 3 );
    
    function remove_thumbnail_dimensions( $html, $post_id, $post_image_id ) {
        $html = preg_replace( '/(width|height)="d*"s/', "", $html );
        return $html;
    } 
    
  6. //all classes i need in a string
    
    $classes = 'img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image'; 
    
    //then i use my variable
    
    the_post_thumbnail('post_thumbnail', array( 'class' => $classes ));
    
  7. I have the following code, and as the posts are from previous versions … They did not work …

    What should I do?

    <figure class="image"><img class="lazyload p402_hide" alt="" 
     width="800" height="400" data-sizes="auto" data- 
    src="https://br.clubnation.club/wp-content/uploads/2020/01/primeira- 
    loja-oficial-de-harry-potter-sera-aberta-em-nova-york-2.jpg" data- 
     srcset="https://br.clubnation.club/wp- 
     content/uploads/2020/01/primeira-loja-oficial-de-harry-potter-sera-aberta- 
     em-nova-york-2.jpg 328w, https://br.clubnation.club/wp- 
     content/uploads/2020/01/primeira-loja-oficial-de-harry-potter-sera- 
     aberta-em-nova-york-3.jpg 380w, https://br.clubnation.club/wp- 
     content/uploads/2020/01/primeira-loja-oficial-de-harry-potter-sera- 
     aberta-em-nova-york-4.jpg 528w, https://br.clubnation.club/wp- 
     content/uploads/2020/01/primeira-loja-oficial-de-harry-potter-sera- 
     aberta-em-nova-york-5.jpg 704w" />
     <figcaption>A loja será inaugurada no próximo verão.
     (Fonte: Warner Bros/Divulgação)</figcaption></figure>