Implement automatic OpenGraph tags population

This commit is contained in:
Vencislav Atanasov 2023-09-08 13:17:11 +03:00
parent 4231dab898
commit aa75f2f60f
1 changed files with 36 additions and 37 deletions

View File

@ -8,19 +8,18 @@ add_filter( 'the_excerpt', 'shortcode_unautop');
add_filter( 'the_excerpt', 'do_shortcode'); add_filter( 'the_excerpt', 'do_shortcode');
// OpenGraph image for FB/Twitter // OpenGraph image for FB/Twitter
// TODO: less hacky solution
function og_image( $tags ) { function og_image( $tags ) {
$og_image = esc_url("https://www.openfest.org/2023/wp-content/uploads/sites/27/2023/09/2023_fb_preview.jpg"); $imagePath = __DIR__ . '/img/' . $blog_slug . '_fb_preview.jpg';
unset( $tags['og:image'] ); if (file_exists($imagePath)) {
$tags['og:image'] = $og_image; $imageUrl = esc_url('https://www.openfest.org/wp-content/themes/initfest/img/' . basename($imagePath));
unset( $tags['og:image:width'] ); list($width, $height) = getimagesize($imagePath);
$tags['og:image:width'] = 678;
unset( $tags['og:image:height'] );
$tags['og:image:height'] = 260;
unset( $tags['twitter:image'] ); $tags['og:image'] = $imageUrl;
$tags['twitter:image'] = $og_image; $tags['og:image:width'] = $width;
$tags['og:image:height'] = $height;
$tags['twitter:image'] = $imageUrl;
}
return $tags; return $tags;
} }