léonardrodriguez
Menu

How to Add Facebook Open Graph to WordPress

An archived WordPress tutorial on Open Graph metadata for Facebook previews: titles, descriptions, images, and page URLs.

Open Graph metadata and link previews
Open Graph metadata controls the preview of a shared link.

When someone liked or shared a page on Facebook, a preview could appear on their wall. Open Graph metadata let you specify the article title, thumbnail, and description, as in this example:

Title, thumbnail, and description in a Facebook link preview

For the classic WordPress setup covered here, this involved adding a few meta tags to the theme’s header.php file.

1. Find your Facebook admin ID and app ID

Before editing header.php, you needed your Facebook admin ID and app ID. The instructions used the Facebook Insights page.
Click ‘Insights for Your Website’ in the upper-right corner, select the relevant page, and note the ID.
The screenshots below show the process.
Your fb:app_id:

facebook insights

Your fb:admins ID:

fb_admin_id

You also need the URL of your website’s main logo. If you don’t have one, create an image and upload it to your WordPress theme’s images folder.

2. Add the meta tags to header.php

Here’s what each field controls. Title is the page or article title shown in the Facebook preview. Description is the text displayed below it.

Type identifies the kind of content under the Open Graph protocol; the original tutorial discussed blog, article, and website. URL is the page address. Image specifies the preview image. Site_name is your website’s name.

Insert the following code into header.php, replacing fb:app_id and fb:admins with your own values. The first line may already exist in your theme: check it before adding the same line twice.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <html xmlns=”http://www.w3.org/1999/xhtml” xmlns:fb=”http://www.facebook.com/2008/fbml” xmlns:og=”http://opengraphprotocol.org/schema/” <?php language_attributes(); ?>> <?php if (have_posts()):while(have_posts()):the_post();endwhile;endif;?> <!– Facebook Opengraph –> <meta property=”fb:app_id” content=”your_app_id” /> <meta property=”fb:admins” content=”your_admin_id” /> <meta property=”og:url” content=”<?php the_permalink() ?>”/> <?php if (is_single()) { ?> <meta property=”og:title” content=”<?php single_post_title(”); ?>” /> <meta property=”og:description” content=”<?php echo strip_tags(get_the_excerpt($post->ID)); ?>” /> <meta property=”og:type” content=”article” /> <meta property=”og:image” content=”<?php echo wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) ) ?>” /> <?php } else { ?> <meta property=”og:site_name” content=”<?php bloginfo(‘name’); ?>” /> <meta property=”og:description” content=”<?php bloginfo(‘description’); ?>” /> <meta property=”og:type” content=”website” /> <meta property=”og:image” content=”<?php bloginfo(‘template_url’) ?>/path/to-your/logo.jpg” /> <?php } ?>

Next, read how to add the Facebook Like button to WordPress.

HistoryPublication, updates and clarifications
  1. Update
  2. First published

Keep exploring.

All articles ↗

SUGGEST A CORRECTION

Spotted an error?

Tell me which passage needs correcting. Include a source if you can, so I can check it.

We use the information you provide to respond to your message. How your data is used