Skip to content

Instantly share code, notes, and snippets.

@Anasxrt
Last active February 20, 2026 19:00
Show Gist options
  • Select an option

  • Save Anasxrt/25c4aca50312cf30c62c2118b5c8de1b to your computer and use it in GitHub Desktop.

Select an option

Save Anasxrt/25c4aca50312cf30c62c2118b5c8de1b to your computer and use it in GitHub Desktop.
/**
* Force JPG for Yoast SEO Social Meta
* This fixes the AVIF preview issue on LINE and Facebook.
*/
// 1. Fix the Image URL in Open Graph and Twitter Tags
add_filter( 'wpseo_opengraph_image', 'cmn_yoast_force_jpg', 99 );
add_filter( 'wpseo_twitter_image', 'cmn_yoast_force_jpg', 99 );
function cmn_yoast_force_jpg( $url ) {
if ( empty( $url ) ) return $url;
// Swap .avif extension for .jpg
return str_replace( '.avif', '.jpg', $url );
}
// 2. Fix the Image Type Tag (Ensure it says image/jpeg)
add_filter( 'wpseo_opengraph_image_type', function( $type ) {
return 'image/jpeg';
}, 99 );
// 3. The "Safety Net" Buffer (In case a performance plugin rewrites the HTML last)
add_action( 'template_redirect', function() {
ob_start( function( $buffer ) {
return preg_replace_callback(
'/<meta property="og:image" content="([^"]+)\.avif"/i',
function( $m ) {
return '<meta property="og:image" content="' . $m[1] . '.jpg"';
},
$buffer
);
});
});
@Anasxrt
Copy link
Author

Anasxrt commented Feb 20, 2026

You can add this snippet to your theme's functions.php file, or use a plugin like Code Snippets or Perfmatters. After adding the code, make sure to clear the social media cache. For Facebook or Line, you can re-scrape your URL using the Facebook Debug Tool to update the preview.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment