Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save KustomDeveloper/02ad7022fb343bae1eeffb2110c0bc74 to your computer and use it in GitHub Desktop.

Select an option

Save KustomDeveloper/02ad7022fb343bae1eeffb2110c0bc74 to your computer and use it in GitHub Desktop.
Add open graph (og:) tags to wordpress for facebook sharing of posts
<?php if( is_single() ) { ?>
<?php
function ogtrimtext($data, $limit) {
$arr = explode(" ", $data);
$new_arr = array_slice($arr, 0, $limit);
return implode(" ", $new_arr);
}
global $post;
$og_post_object = get_post( $post_id );
$og_title = get_the_title($post->ID);
$og_content = $post->post_content;
$og_desc = preg_replace("/<img[^>]+\>/i", "", $og_content);
$og_excerpt = trim(ogtrimtext($og_desc, 15));
$og_thumburl = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ), 'full' );
$og_filetype = wp_check_filetype($og_thumburl);
if(has_post_thumbnail( $post->ID, 'full' )) { ?>
<meta property="og:image" content="<?php echo $og_thumburl; ?>">
<meta property="og:image:type" content="<?php echo $og_filetype['type']; ?>">
<meta property="og:image:width" content="100%">
<meta property="og:image:height" content="100%">
<meta property="og:type" content="article">
<meta property="og:title" content="<?php echo $og_title; ?>">
<meta property="og:description" content="<?php echo $og_excerpt; ?>">
<?php } ?>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment