Search code examples
phpwordpressstr-replace

Replace extension on thumbnails


I'm trying to do something in wordpress, by default I'm using webp images, i succeeded using the_content filter on post pages, but not on featured images on home pages.

This is the code i use on post pages;

<?php 

add_filter('the_content', 'change_img', 99);
function change_img( $content )
{
    return str_replace('.webp', '.jpg', $content);
} ?>

This is the code i use to show featured images on homepage

<ul>

    <?php $ksf = new WP_Query( 'posts_per_page=8' ); ?>
  <?php while ($ksf -> have_posts()) : $ksf -> the_post(); ?>

            <li>
                <a href="<?php the_permalink() ?>">
            
<figure>
        <?php if( !empty(get_the_post_thumbnail()) ) {
    $feat_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "full", true);
    
    ?>
    <img  src="<?php echo (($feat_image[0]))?>" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); ?>" width="750" height="422" />
    <?php  } ?>
    <figcaption>
        <?php the_title(); ?>
    </figcaption>
                </figure>   </a>    
            

            </li>
                        

        <?php endwhile;
wp_reset_postdata(); ?></ul>

Solution

  • Replace with this;

    <?php 
    
    add_filter('wp_get_attachment_image_src', 'change_img', 99);
    function change_img( $image )
    {
        return str_replace('.webp', '.jpg', $image);
    } ?>
    

    I tested it with the code you have.