Skip to content

Instantly share code, notes, and snippets.

@jamiebergen
Created March 20, 2018 21:10
Show Gist options
  • Save jamiebergen/ef2bc409b7f0796884cba51698f83bf0 to your computer and use it in GitHub Desktop.
Save jamiebergen/ef2bc409b7f0796884cba51698f83bf0 to your computer and use it in GitHub Desktop.
[Social Sharing Shortcode] Generates social sharing markup #bixler #social
// Shortcode for social sharing
// [red-social-sharing]
function social_sharing_shortcode() {
global $post;
// Get current page URL
$page_url = urlencode(get_permalink());
// Get current page title
$page_title = str_replace( ' ', '%20', get_the_title());
// Construct sharing URL without using any script
$twitter_url = 'https://twitter.com/intent/tweet?text='.$page_title.'&url='.$page_url;
$facebook_url = 'https://www.facebook.com/sharer/sharer.php?u='.$page_url;
$share_markup = '';
$share_markup .= '<div class="red-ssba red-ssba-wrap">';
$share_markup .= '<a class="red-ssba_facebook_share" href="'. $facebook_url .'" onclick="window.open(this.href, \'facebook-share\',\'width=580,height=500\');return false;">';
$share_markup .= '<i class="icon-social icon-social-facebook" aria-hidden="true"></i><span class="screen-reader-text">Facebook</span></a>';
$share_markup .= '<a class="red-ssba_twitter_share" href="'. $twitter_url .'" onclick="window.open(this.href, \'twitter-share\', \'width=550,height=500\');return false;">';
$share_markup .= '<i class="icon-social icon-social-twitter" aria-hidden="true"></i><span class="screen-reader-text">Twitter</span></a>';
$share_markup .= '<a class="red-ssba_email_share" data-site="email" target="_blank" href="mailto:?subject='.$page_title.'&body='.$page_url.'">';
$share_markup .= '<i class="icon-social icon-envelope" aria-hidden="true"></i><span class="screen-reader-text">Email</span></a>';
$share_markup .= '</div>';
return $share_markup;
}
add_shortcode('red-social-sharing', 'social_sharing_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment