Using Open Graph Without Jetpack

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

Source Site:

http://www.codecollector.net/view/45A1B7B5-C49E-4BEF-829D-81A39982B264-519-0000044ABE174785

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: 416
Date Added: 2015-10-12 10:12:18
Last Modified: 2015-10-12 04:21:28

Web Analytics