Skip to content

Instantly share code, notes, and snippets.

@Shumaher
Last active April 24, 2018 14:42
Show Gist options
  • Save Shumaher/e4932ce2cc4f2268f66b4a520fec49e6 to your computer and use it in GitHub Desktop.
Save Shumaher/e4932ce2cc4f2268f66b4a520fec49e6 to your computer and use it in GitHub Desktop.
Open Graph tags for WordPress, see http://blog.alexz.me/wp-opengraph for more info
<head>
<?php if (is_single() || is_page()) { ?>
<meta name="description" content="<?php $s = trim(str_replace("\r\n", " ", htmlspecialchars(strip_tags($post->post_content)))); if(mb_strlen($s,'UTF-8') > 200){$postdescr = mb_substr($s,0,198,'UTF-8').' …';} else{$postdescr = $s;}; echo $postdescr; ?>" />
<meta property="og:description" content="<?php echo $postdescr; ?>" />
<meta property="og:url" content="http://site.com/<?php global $post; echo $post->post_name; ?>" /> <!-- OR -->
<meta property="og:url" content="<?php the_permalink() ?>" />
<meta property="og:title" content="Site Name<?php wp_title(); ?>" />
<meta property="og:headline" content="Site Name<?php wp_title(); ?>" />
<?php if (get_post_meta($post->ID, 'image_id', true)){$postimage = wp_get_attachment_image_src(get_post_meta($post->ID, 'image_id', true), 'full'); ?>
<link rel="image_src" href="<?php echo $postimage[0]; ?>" />
<meta property="og:image" content="<?php echo $postimage[0]; ?>" />
<meta property="og:image:width" content="<?php echo $postimage[1]; ?>" />
<meta property="og:image:height" content="<?php echo $postimage[2]; ?>" />
<?php } elseif(preg_match('#^.*<img.*src="(.*)"#siU', $post->post_content, $imgsrc) AND preg_match('#^.*<img.*width="([0-9]+)"#siU', $post->post_content, $imgwidth) AND preg_match('#^.*<img.*height="([0-9]+)"#siU', $post->post_content, $imgheight)) { ?>
<link rel="image_src" href="<?php echo $imgsrc[1]; ?>" />
<meta property="og:image" content="<?php echo $imgsrc[1]; ?>" />
<meta property="og:image:width" content="<?php echo $imgwidth[1]; ?>" />
<meta property="og:image:height" content="<?php echo $imgheight[1]; ?>" />
<?php } else { ?>
<link rel="image_src" href="http://site.com/logo.png" />
<meta property="og:image" content="http://site.com/logo.png" />
<meta property="og:image:width" content="640" />
<meta property="og:image:height" content="320" />
<?php } ?>
<meta property="og:type" content="article" />
<meta property="article:author" content="https://www.facebook.com/FacebookUsername" /> <!-- OR -->
<meta property="article:publisher" content="https://www.facebook.com/FacebookPage" />
<meta property="article:published_time" content="<?php echo date('c', strtotime($post->post_date)); ?>" />
<?php } else { ?>
<meta name="description" content="Site Name » Site Tagline" />
<meta property="og:description" content="Site Name » Site Tagline" />
<meta property="og:url" content="<?php global $wp; echo $current_url = $current_uri = home_url(add_query_arg(NULL, NULL)); ?>" />
<meta property="og:title" content="Site Name<?php wp_title(); ?>" />
<meta property="og:headline" content="Site Name<?php wp_title(); ?>" />
<link rel="image_src" href="http://site.com/logo.png" />
<meta property="og:image" content="http://site.com/logo.png" />
<meta property="og:image:width" content="640" />
<meta property="og:image:height" content="320" />
<meta property="og:type" content="website" /> <!-- OR -->
<meta property="og:type" content="blog" />
<?php } ?>
<meta property="og:site_name" content="Site Name" />
<meta property="profile:first_name" content="FirstName" />
<meta property="profile:last_name" content="LastName" />
<meta property="fb:admins" content="123456789012345" />
<meta property="fb:app_id" content="123456789012345" />
<meta property="twitter:card" content="summary" />
<meta property="twitter:creator" content="@TwitterUsername" /> <!-- OR -->
<meta property="twitter:site" content="@SiteTwitterUsername" />
</head>
@interactiveRob
Copy link

Super helpful to list all these out in one place. thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment