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>
My Answer:
php
function replace_webp_extension( $content ) {
$content = str_replace( '.webp', '.jpg', $content );
return $content;
}
add_filter( 'the_content', 'replace_webp_extension' );
_If you want to replace the extension on featured images on home pages, you can use the following code:_
php
function replace_webp_extension_featured_image( $content ) {
if ( is_home() || is_front_page() ) {
$content = str_replace( '.webp', '.jpg', $content );
}
return $content;
}
add_filter( 'post_thumbnail_html', 'replace_webp_extension_featured_image' );
_This code checks if the current page is the home page or front page using `is_home()` and `is_front_page()` functions, and then replaces the `.webp` extension with `.jpg` on the featured image.
Rate this post
3 of 5 based on 1476 votesComments