Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chasereeves/e670d6496b3b1eb945c7 to your computer and use it in GitHub Desktop.
Save chasereeves/e670d6496b3b1eb945c7 to your computer and use it in GitHub Desktop.
Wordpress Function: Social Meta Tags for Open Graph
function insert_fb_in_head() {
global $post;
if ( !is_singular()) return;
$meta = strip_tags($post->post_content);
$meta = strip_shortcodes($post->post_content);
$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
$meta = substr($meta, 0, 125);
$meta = strip_tags($meta);
// basics, same for all
echo '<meta property="og:locale" content="en_US" />' . "\n";
echo '<meta property="og:type" content="article" />' . "\n";
echo '<meta name="twitter:creator" content="@fizzle"/>' . "\n";
echo '<link rel="publisher" href="https://plus.google.com/b/112331585854758294372/+FizzleCo/posts"/>' . "\n";
echo '<meta name="twitter:site" content="@fizzle"/>' . "\n";
echo '<meta name="twitter:domain" content="Fizzle"/>' . "\n";
echo '<meta property="og:site_name" content="Fizzle" />' . "\n";
echo '<meta property="article:publisher" content="https://www.facebook.com/FizzleCo" />' . "\n";
echo '<meta property="fb:admins" content="793120244" />' . "\n";
echo '<meta name="twitter:card" content="summary_large_image"/>' . "\n";
// title, URL, description
echo '<meta property="og:title" content="' . get_the_title() . '"/>' . "\n";
echo '<meta property="og:url" content="' . get_permalink() . '"/>' . "\n";
echo '<meta property="og:description" content="' . $meta . '"/>' . "\n";
echo '<meta name="description" content="' . $meta . '">' . "\n";
// post image
if(!has_post_thumbnail( $post->ID )) {
if ( post_is_podcast() ) {
echo '<meta property="og:image" content="https://fizzle.co/wp-content/themes/fizzle_02/images/sparkline-fizzle-show-post-image-06.jpg"/>' . "\n";
}
else {
echo '<meta property="og:image" content="https://fizzle.co/wp-content/uploads/2015/02/fizzle-ogimage-01.jpg"/>' . "\n";
}
}
else{
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>' . "\n";
}
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment