I added the following code in functions.php (wordpress) to add metadata dynamically as it differs on every page.

//facebook opengraph

//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');

//Lets add Open Graph Meta Info

function insert_fb_in_head() {
    global $post;
    if ( !is_singular()) //if it is not a post or a page
        return;
        echo '<meta property="fb:app_id" content="1317281045016237"/>';
        echo '<meta property="og:title" content="' . get_the_title() . '"/>';
        echo '<meta property="og:type" content="article"/>';
        echo '<meta property="og:url" content="' . get_permalink() . '?fbrefresh=CAN_BE_ANYTHING"/>';
        echo '<meta property="og:site_name" content="EXN LUBE"/>';
        echo '<meta property="og:image:width" content="100"/>';
        echo '<meta property="og:image:height" content="100"/>';
    if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
        $default_image="http://new2.exnlube.com/wp-content/uploads/2017/04/Marine-1.jpg"; //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_src( get_post_thumbnail_id( $post->ID ), 'medium' );
        echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
    }
    echo "
";
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );

Previously I added the Open Graph meta data in header directly which was not dynamic for testing purposes.

Now as I scrap for new metadata in debugger, it shows the correct one. But when I click the share button on my site, it still shows the old meta data which doesn’t exist any more.

How do I make sure, new meta data is fetched on every page that I click share button?

Read more here: Facebook Open Graph still has the old meta data while debugger shows the latest


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: