Facebook “like” open graph meta in header.php

I would like to add open graph meta tags to my header.php, but can someone explain how this works if header.php comes before my loop with the relevant information??

<meta property="og:title" content="The Rock"/>
<meta property="og:type" content="movie"/>
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/>
<meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>
<meta property="og:site_name" content="IMDb"/>
<meta property="fb:admins" content="USER_ID"/>
<meta property="og:description"
          content="A group of U.S. Marines, under command of
                   a renegade general, take over Alcatraz and
                   threaten San Francisco Bay with biological
                   weapons."/>

For example:

Read More
<meta property="og:description" content="<?php the_content(); >?"/>

Won’t work because the header.php file comes before the Loop…any advice?

Related posts

Leave a Reply

1 comment

  1. I have this class from a while ago which still works fine on posts and pages using admin_head action hook

    <?php
    /**
     * Simple facebook open graph class for WordPress
     * 
     * @author Ohad Raz 
     * @version 0.1
     * 
     */
    class simple_open_graph{
    
        /**
         * Holds the default image url
         * @var string
         * @since 0.1
         */
        public $default_image_url;
    
        /**
         * holds facebook user id for insights (analytics of the Like Buttons)
         * @var string
         * @since 0.1
         */
        public $facebook_account_id;
    
        /**
         * Class Constructor
         * 
         * @since 0.1
         * @author Ohad Raz
         * @access public
         * 
         * @return Void
         */
        public function __construct(){
            $this->default_image_url = "http://www.example.com/image.jpg";
            $this->facebook_account_id = "Your Facebook Account ID";
    
            add_action( 'wp_head', array($this,'facebook_open_graph_meta_head'), 5 );
        }
    
        /**
         * function to get the content type
         * 
         * @since 0.1
         * @author Ohad Raz
         * @access public
         * 
         * @return (string) either website or article based on the curent page
         */
        public function content_type(){
            if (is_single() || is_page())
                return "article"; 
            else 
                return "website";
        }
    
        /**
         *  function to get the image from post_thumb, post content or default image
         *  
         *  @since 0.1
         * @author Ohad Raz
         * @access public
         * 
         * @return (string) image src
         */
        public function graph_image(){
            if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) {
                $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '', '' );
                return $src[0];
            } else {
                global $post, $posts;
                $fbimage = '';
                $output = preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i',
                $post->post_content, $matches);
                return $matches [1] [0];
            }
    
            return $this->$default_image_url;
        }
    
        /**
         * Function that actually prints the open graph meta tags
         *   
         * @since 0.1
         * @author Ohad Raz
         * @access public
         * 
         * @return Void
         */
        public function facebook_open_graph_meta_head() {
            global $post;
            echo '<meta property="fb:admins" content="'. $facebook_account_id .'"/>'; ?>
            <meta property="og:title" content="
            <?php 
                if(is_home()) {
                    bloginfo('name'); 
                } elseif(is_category()) { 
                    echo single_cat_title();
                } elseif(is_author()) { 
                    $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author')); 
                    echo $curauth->display_name; 
                } else { 
                    echo the_title(); 
                } ?>" />
            <meta property="og:description" content="<?php echo strip_tags(get_the_excerpt($post->ID)); ?>"/>
            <meta property="og:url" content="<?php the_permalink(); ?>"/>
            <meta property="og:image" content="<?php echo $this->graph_image(); ?>"/>
            <meta property="og:type" content="<?php echo $this->content_type(); ?>"/>
            <meta property="og:site_name" content="<?php bloginfo('name'); ?>"/>
            <?php
        }
    
    }//end class