Skip to content

Instantly share code, notes, and snippets.

@nmyers
Last active March 24, 2017 17:01
Show Gist options
  • Save nmyers/0bc7aea5bcf402d5bf11 to your computer and use it in GitHub Desktop.
Save nmyers/0bc7aea5bcf402d5bf11 to your computer and use it in GitHub Desktop.
Social share code
// Add to functions.php
function add_opengraph_markup() {
if (is_single()) {
global $post;
if(get_the_post_thumbnail($post->ID, 'thumbnail')) {
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_object = get_post($thumbnail_id);
$image = $thumbnail_object->guid;
} else {
// set default image
$image = ''; //
}
//$description = get_bloginfo('description');
$description = substr(strip_tags($post->post_content),0,200) . '...';
?>
<meta property="og:title" content="<?php the_title(); ?>" />
<meta property="og:type" content="article" />
<meta property="og:image" content="<?=$image?>" />
<meta property="og:url" content="<?php the_permalink(); ?>" />
<meta property="og:description" content="<?=$description?>" />
<meta property="og:site_name" content="<?=get_bloginfo('name')?>" />
<?php
}
}
add_action('wp_head', 'add_opengraph_markup');
<div class="social-share" >
<ul>
<li><h6>SHARE:</h6></li>
<li><a class="twitter" target="_blank" href="https://twitter.com/share?url=<?=rawurlencode(get_permalink())?>&text=<?=rawurlencode(get_the_title().' – ')?>">
<span class="icon-twitter"></span><span class="visuallyhidden" >Twitter</span>
</a></li>
<li><a target="_blank" href="http://www.facebook.com/sharer.php?u=<?=rawurlencode(get_permalink())?>" >
<span class="icon-facebook"></span><span class="visuallyhidden" >Facebook</span>
</a></li>
<li><a target="_blank" href="/cdn-cgi/l/email-protection#650008040c09405755040101170016164057550d0017005a1610070f00061158595a58170412101709000b060a01004d42260d00060e45110d0c16450a10115f452b0009091c4527000b452d041c0a100b45361110010c0a4587e5f645424b0200113a110d003a110c1109004d4c4c5a5b43070a011c58595a58170412101709000b060a01004d0200113a1500170804090c0b0e4d4c4c5a5b">
<span class="icon-mail"></span><span class="visuallyhidden" >Email</span></a></li>
</ul>
</div><script>/* <![CDATA[ */(function(d,s,a,i,j,r,l,m,t){try{l=d.getElementsByTagName('a');t=d.createElement('textarea');for(i=0;l.length-i;i++){try{a=l[i].href;s=a.indexOf('/cdn-cgi/l/email-protection');m=a.length;if(a&&s>-1&&m>28){j=28+s;s='';if(j<m){r='0x'+a.substr(j,2)|0;for(j+=2;j<m&&a.charAt(j)!='X';j+=2)s+='%'+('0'+('0x'+a.substr(j,2)^r).toString(16)).slice(-2);j++;s=decodeURIComponent(s)+a.substr(j,m-j)}t.innerHTML=s.replace(/</g,'&lt;').replace(/\>/g,'&gt;');l[i].href='mailto:'+t.value}}catch(e){}}}catch(e){}})(document);/* ]]> */</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment