Using Open Graph Without Jetpack

Using Open Graph protocol without jetpack on wordpress
language: PHP
license: Other

Code for Snippet:

                
function wp_opengraph_for_posts() {
    if ( is_singular() ) {
        global $post;
        setup_postdata( $post );
        $output = '<meta property="og:type" content="article" />' . "\n";
        $output .= '<meta property="og:title" content="' . esc_attr( get_the_title() ) . '" />' . "\n";
        $output .= '<meta property="og:url" content="' . get_permalink() . '" />' . "\n";
        $output .= '<meta property="og:description" content="' . esc_attr( get_the_excerpt() ) . '" />' . "\n";
        if ( has_post_thumbnail() ) {
            $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
            $output .= '<meta property="og:image" content="' . $imgsrc[0] . '" />' . "\n";
        }
        echo $output;
    }
}
add_action( 'wp_head', 'wp_opengraph_for_posts' );
 
By the way, you need to update the <html> tag of your theme, too. The <html> tag of my theme’s header.php is like this:
 
<html <?php language_attributes(); ?>>
So I need to update it to look like this:
 
<html <?php language_attributes(); ?> prefix="og: http://ogp.me/ns#">
And that’s it, your posts and pages are now compatible with Open Graph!
comments powered by Disqus

Info

Tags: Wordpress

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 1568
Date Added: 2013-03-08 15:35:34
Last Modified: 2013-03-15 10:23:00

Web Analytics