0

I follow the tutorial in How to Add Facebook Open Graph Meta Data in WordPress Themes and add some echo code in functions.php both in the main theme and child theme. However, when I check the source code of an individual page, the tags don't show up. Why is that?

The script is below:

//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:admins" content="Ooker"/>';
        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="Quả cầu"/>';
    if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
        $default_image="http://example.com/image.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 );
  • Does your theme use wp_head in header file? – Krzysiek Dróżdż Aug 17 at 6:08
  • @KrzysiekDróżdż there is a line <?php wp_head(); ?> in the head section of header.php, so yes – Ooker Aug 17 at 6:28
  • 1
    If you set define( 'WP_DEBUG', true ); and define( 'WP_DEBUG_LOG', true ); in wp-config.php do you see any errors or notices in wp-content\debug.log? – jg314 Aug 19 at 16:44

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.