jmccole83
3/7/2018 - 11:15 PM

Dynamic Share Links

add the following snippet to any post or page template to add dynamic social media share buttons to the page.

'A' tags have classes to identify them as social media links, and to then identify them for singular styling.

<?php $title = get_the_title(); $newTitle = str_replace(' ', '%20', $title); ?>
<a class="fb sm" href="https://www.facebook.com/sharer/sharer.php?u=<?php echo get_permalink(); ?>&title=<?php echo $newTitle; ?>" target="_blank">
  <span class="sr-only">share to facebook - opens a new tab/window</span>
</a>
<a class="tw sm" href="https://twitter.com/intent/tweet?text=<?php echo $newTitle; ?>+<?php echo get_permalink(); ?>" target="_blank">
  <span class="sr-only">share to twitter - opens a new tab/window</span>
</a>
<a class="li sm" href="https://www.linkedin.com/shareArticle?mini=true&url=<?php echo get_permalink(); ?>&title=<?php echo $newTitle; ?>&source=<?php echo get_bloginfo('url'); ?>" target="_blank">
  <span class="sr-only">share to linkedin - opens a new tab/window</span>
</a>
<a class="pi sm" href="http://pinterest.com/pin/create/button/?url=<?php echo get_permalink(); ?>&description=<?php echo $newTitle; ?>" target="_blank">
    <span class="sr-only">share to pinterest - opens in a new tab window</span>
</a>
<a class="gp sm" href="https://plus.google.com/share?url=<?php echo get_permalink();  ?>&media=<?php echo get_permalink(); ?>" target="_blank">
  <span class="sr-only">Share on Google+</span>
</a>