0

After updating WordPress to the new version 4.7 whenever I try to share a product or other link from the website, its image doesn't show up.

I've already already scraped the Facebook link from debugger on Facebook.

I used this code to bring the image, but with this one, I'm receiving just a blank image

//Adding the Open Graph in the Language Attributes
function add_opengraph_doctype($output)
{
    return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"';
}

add_filter('language_attributes', 'add_opengraph_doctype');

//add Open Graph Meta Info
function wh_fb_opengraph() {
    global $post;
    $site_name = get_bloginfo('name');
    $site_desc = get_bloginfo('description');
    //for home page
    ?>
    <meta property="fb:app_id" content="YOUR APPID" /><!-- Optional -->
    <?php
    if (is_home() && is_front_page()) {
        $img_home_page = 'http://example.com/yourimg.jpg'
        ?>
        <meta property="og:type" content="website" />
        <meta property="og:title" content="<?php echo $site_name; ?>"/>
        <meta property="og:description" content="<?php echo $site_desc; ?>"/>
        <meta property="og:url" content="<?= get_site_url(); ?>"/>
        <meta property="og:site_name" content="<?php echo $site_name; ?>"/>
        <meta property="og:image" content="<?php $img_home_page; ?>"/>
        <?php
    }
    //for singles post page
    else if (is_single() && !is_product()) {
        //get featured image of post
        $img_src = wp_get_attachment_url(get_post_thumbnail_id($post->ID), 'medium');

        if ($excerpt = $post->post_excerpt) {
            $excerpt = strip_tags($post->post_excerpt);
            $excerpt = str_replace("'", "", $excerpt);
        } else {
            $excerpt = $site_desc;
        }
        ?>
        <meta property="og:type" content="article" />
        <meta property="og:title" content="<?php echo get_the_title($post->ID); ?>"/>
        <meta property="og:description" content="<?php echo $excerpt; ?>"/>
        <meta property="og:url" content="<?php echo get_the_permalink($post->ID); ?>"/>
        <meta property="og:site_name" content="<?php echo $site_name; ?>"/>
        <meta property="og:image" content="<?php echo $img_src; ?>"/>
        <meta property="article:published_time" content="<?php echo get_the_date('c', $post->ID); ?>" />
        <?php
    }
    //for singles product page only
    elseif (is_product()) {
        $product = get_product($post->ID);
        $stock = ($product->is_in_stock()) ? 'instock' : 'oos';
        $excerpt = $product->post->post_content;
        if ($excerpt) {
            $excerpt = substr(str_replace("'", "", strip_tags($excerpt)), 160);
        } else {
            $excerpt = $site_desc;
        }
        $img_url = wp_get_attachment_image_src(get_post_thumbnail_id($product->ID));
        ?>
        <meta property="og:type" content="product" />
        <meta property="og:title" content="<?php echo get_the_title($post->ID); ?>" />
        <meta property="og:description" content="<?php echo $excerpt; ?>" />
        <meta property="og:url" content="<?php echo get_the_permalink($post->ID); ?>" />
        <meta property="og:site_name" content="<?php echo $site_name; ?>"/>
        <meta property="og:price:amount" content="<?php echo $product->get_price(); ?>" />
        <meta property="og:price:currency" content="<?php echo get_woocommerce_currency(); ?>" />
        <meta property="og:availability" content="<?php echo $stock; ?>" />
        <meta property="og:image" content="<?php echo $img_url[0]; ?>"/>
        <?php
    } else {
        return;
    }
}

add_action('wp_head', 'wh_fb_opengraph', 5);
  • Do you get a blank image for products, posts and homepage share? – Jack Johansson Feb 12 '17 at 16:52
  • Yes, for all of them – Bursuc Andrei Feb 12 '17 at 16:56
  • That is strange. You already have a static image link for your homepage. Is the meta code correctly injected to your header? – Jack Johansson Feb 12 '17 at 16:57
  • This is something that I don't know.. – Bursuc Andrei Feb 12 '17 at 16:57
  • An HTML output of your header or a link to your page would be nice to investigate. – Jack Johansson Feb 12 '17 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.