How to Get Pinterest OpenGraph Info onto Your WordPress Site (Including for Woocommerce!)

If you want to make the most of Pinterest, you’ll need to add OpenGraph data to your site. There are plenty of plugins that can do this for you, but if you want to write the code yourself, save the plugin headaches, and likely reduce the overhead most plugins add, this is quite simple.

The biggest reason to take the time to add the data Pinterest is requesting is simply due to the fact that this somewhat obscure (to many of us!) social network tends to drive more traffic than Facebook and the other players combined.

I put absolutely no time whatsoever into Pinterest, and I see thousands of individual users referred back to my most popular sites that use photographs every month. There are two primary places I believe you’ll want Pinterest OpenGraph data, on Posts and (if you’re running Woocommerce) Products. You may also want them on Pages and custom post types as well. I’ll explain all of this.

Firstly, though, if you just want them on Posts and Products, you can use this code (it would be added as a function linked to wp_head via functions.php, or you could add it directly to your header.php file, before the closing </head> tag.)

<?php if (is_singular('post') || is_singular('product')) {
if (is_singular('post')) {
$og_type = 'article';
} elseif (is_singular('product')) {
$og_type = 'product';
} ?>
<meta property="og:type" content="<?php echo $og_type; ?>" />
<meta property="og:title" content="<?php echo get_the_title(); ?>" />
<meta property="og:description" content="<?php echo get_the_excerpt(); ?>" />
<meta property="og:url" content="<?php echo get_permalink(); ?>" />
<meta property="og:site_name" content="<?php echo get_bloginfo('name'); ?>" />
<?php if (is_singular('post')) { ?>
<meta property="article:published_time" content="<?php the_time('Y-m-d'); ?>T<?php the_time('G:i:sP'); ?>" />
<meta property="article:author" content="<?php echo get_the_author_meta('display_name'); ?>" />
<?php } if (is_singular('product')) { ?>
<meta property="product:price:amount" content="<?php echo get_post_meta(get_the_ID(), '_price', true); ?>" />
<meta property="product:price:currency" content="USD" />
<meta property="og:availability" content="instock" />
<?php } ?>
<?php } ?>

Or if you only want posts:

<?php if (is_singular('post')) { ?>
<meta property="og:type" content="article" />
<meta property="og:title" content="<?php echo get_the_title(); ?>" />
<meta property="og:description" content="<?php echo get_the_excerpt(); ?>" />
<meta property="og:url" content="<?php echo get_permalink(); ?>" />
<meta property="og:site_name" content="<?php echo get_bloginfo('name'); ?>" />
<meta property="article:published_time" content="<?php the_time('Y-m-d'); ?>T<?php the_time('G:i:sP'); ?>" />
<meta property="article:author" content="<?php echo get_the_author_meta('display_name'); ?>" />
<?php } ?>

Or to include both Posts and Pages:

<?php if (is_singular('post') || is_page()) { ?>
<meta property="og:type" content="article" />
<meta property="og:title" content="<?php echo get_the_title(); ?>" />
<meta property="og:description" content="<?php echo get_the_excerpt(); ?>" />
<meta property="og:url" content="<?php echo get_permalink(); ?>" />
<meta property="og:site_name" content="<?php echo get_bloginfo('name'); ?>" />
<meta property="article:published_time" content="<?php the_time('Y-m-d'); ?>T<?php the_time('G:i:sP'); ?>" />
<meta property="article:author" content="<?php echo get_the_author_meta('display_name'); ?>" />
<?php } ?>

Or if you want to use it on custom post types as well, note the first line’s is_singular() area:

<?php if (is_singular(array('post', 'some_custom_post_type'))) { ?>
<meta property="og:type" content="article" />
<meta property="og:title" content="<?php echo get_the_title(); ?>" />
<meta property="og:description" content="<?php echo get_the_excerpt(); ?>" />
<meta property="og:url" content="<?php echo get_permalink(); ?>" />
<meta property="og:site_name" content="<?php echo get_bloginfo('name'); ?>" />
<meta property="article:published_time" content="<?php the_time('Y-m-d'); ?>T<?php the_time('G:i:sP'); ?>" />
<meta property="article:author" content="<?php echo get_the_author_meta('display_name'); ?>" />
<?php } ?>

That’s it, enjoy your Pinterest mastery!

Up Next: The Big Problem with Yelp! Advertising