ThemeShaper Forums » Thematic


How to: Facebook Open Graph <meta>

(4 posts)
  • Started 12 years ago by Ben W.
  • Latest reply from sunysky
  • This topic is not a support question
  1. Ben W.

    If you are using the Facebook “Like” button on your page, you may want to have a better control over it – including an Admin page. (More about Open Graph protocol.)

    You'll need a logo image for your blog and put it to your Thematic /images/ directory, or, if you are using a child theme, to your child themes /images/ directory. Put the following code snippet to your functions.php of Thematic, or, if you are using a child theme, of your child theme:

    (If you need to find out your Facebook ID, click on the "Photos" tab of your FB profile, select your profile photos, and grab the number of the…&id=YOUR_ID part.)

     * Facebook Open Graph
    function open_graph($content) {
    	$content .='<meta property="fb:admins" content="YOUR_FACEBOOK_ID" />' . "\n";
    	if (is_home() || is_front_page()) {
    		$url = get_option('home');
    		$title = get_bloginfo('name');
    		$description = get_bloginfo('description');
    		$article = 'blog';
    	} else {
    		global $post;
    		$url = get_permalink($post->ID);
    		$title = get_the_title();
    		$description = get_the_excerpt();
    		$article = 'article';
    	$content .='<meta property="og:site_name" content="' . get_bloginfo('name') . '" />' . "\n";
    	$content .='<meta property="og:title" content="' . $title . '" />' . "\n";
    	$content .='<meta property="og:description" content="' . $description . '" />' . "\n";
    	$content .='<meta property="og:type" content="' . $article . '" />' . "\n";
    	$content .='<meta property="og:url" content="' . $url . '" />' . "\n";
    	$content .='<meta property="og:image" content="' . get_bloginfo('stylesheet_directory') . '/images/YOUR_LOGO.jpg" />' . "\n";
    	return $content;
    add_filter ('thematic_create_description', 'open_graph');
    function open_graph_doctype($content) {
    	$content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    	$content .= '<html xmlns="" xmlns:og="" xmlns:fb=""';
    	return $content;
    apply_filter('thematic_create_doctype', 'open_graph_doctype');

    Please note that the document won't validate as XHTML 1.0 Transitional anymore. Changing it to the required DOCTYPE XHTML + RDFa (<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "">) would make it almost like XHTML 1.1, which is Strict, but will easily brake existing forms etc.

    If you are not using a caching solution (like WP Super Cache or Hyper Cache), you could restrict the meta tags to be shown the Facebook crawler only:

    function open_graph($content) {
    	if ($_SERVER['HTTP_USER_AGENT'] == 'facebookexternalhit/1.0 (+') {
    	[the stuff from above]


    Posted 12 years ago #
  2. Hi,

    When I try your solution my wordpress crash and I obtain this error message:

    Fatal error: Call to undefined function apply_filter() in /public_html/photo/wp-content/themes/autofocusplus/functions.php on line 36

    The line 36 is:
    apply_filter('thematic_create_doctype', 'open_graph_doctype');

    I have wordpress 3.0.1 with Autofocus+ theme wich is based on Thematic theme.

    Do you know where is the problem?

    Thank you in advance!

    PS: excuse my english, i'm french...

    Posted 12 years ago #
  3. It should be add_filter.


    Posted 12 years ago #
  4. It works!
    Thank a lot Chris! :)

    Posted 12 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.