Target WooCommerce Product Gallery Image (rather than Featured Image) for og:image
I'm using the following code to set my Open Graph meta data in functions.php
It works great for selecting the featured image, however I would like to set it to use the product gallery image instead (which is better for social media marketing). Here's a screen grab to be more specific: https://snipboard.io/buvHfk.jpg
How can I modify the code below to target the 1st WooCommerce gallery image? I could not find anything on this.
function insert_fb_in_head() {
global $post;
if ( !is_singular()) //if it is not a post or a page
return;
echo 'meta property=og:title content=' . get_the_title() . '/';
echo 'meta property=og:type content=article/';
echo 'meta property=og:url content=' . get_permalink() . '/';
echo 'meta property=og:site_name content=My Website/';
if( isset( $gallery_image_ids[1] ) ) { //the post does not have featured image, use a default image
$default_image=https://www.website.com; //replace this with a default image on your server or an image in your media library
echo 'meta property=og:image content=' . $default_image . '/';
}
else{
$thumbnail_src = wp_get_attachment_image_url( $gallery_image_ids[1], 'single-post-thumbnail');
echo 'meta property=og:image content=' . esc_attr( $thumbnail_src[0] ) . '/';
}
echo
;
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );
Thank you in advance for your help.
Dan
Topic woocommerce-offtopic open-graph gallery Wordpress
Category Web