Skip to content

Instantly share code, notes, and snippets.

@bnecreative
Last active December 15, 2015 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnecreative/aaebaa1829936aa51bd5 to your computer and use it in GitHub Desktop.
Save bnecreative/aaebaa1829936aa51bd5 to your computer and use it in GitHub Desktop.
Simple Share Icons for WordPress - No JS!
/*
* Post Share Buttons
*
* Adds Facebook, Twitter, Google+, and Email share buttons. Links
* will open in a new window if they include the class "popup".
* The configuration of the popup window is via js.
*
* Icons rely on FontAwesome
*
* @url http://petragregorova.com/articles/social-share-buttons-with-custom-icons/
*
*/
function bne_share_buttons() {
// Get current page URL
$page_url = get_permalink();
// Get current page title
$page_title = str_replace( ' ', '%20', get_the_title() );
// Thumbnail
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_id() ), 'full' );
// Construct sharing URL without using any script
$share_urls = apply_filters('bne_get_share_button_links', array(
'facebook' => 'https://www.facebook.com/sharer/sharer.php?u='.$page_url.'&title='.$page_title,
'google' => 'https://plus.google.com/share?url='.$page_url,
'twitter' => 'https://twitter.com/intent/tweet?status='.$page_title.'+'.$page_url,
'email' => 'mailto:?subject='.$page_title.'&body='.$page_url,
'tumblr' => 'http://www.tumblr.com/share?v=3&u='.$page_url.'&t='.$page_title,
'pinterest' => 'http://pinterest.com/pin/create/bookmarklet/?media='.$thumbnail[0].'&url='.$page_url.'&is_video=false&description='.$page_title,
), $page_url, $page_title, $thumbnail );
// Add sharing button at the end of post content
$output = '<div class="share">';
$output .= '<ul class="bne-share clearfix">';
// Loop through Share links.
foreach( $share_urls as $key => $url ) {
// Defaults
$icon = $key;
$popup = 'popup';
// Overrides
if( 'email' == $key ) {
$popup = null;
$icon = 'envelope-o';
}
// Output link
$output .= '<li><a href="'.$url.'" class="btn-share '.$key.' '.$popup.'" data-toggle="tooltip" data-placement="top" title="Share this via '.ucfirst( $key ).'"><i class="fa fa-fw fa-'.$icon.'"></i></a></li>';
}
$output .= '</ul>';
$output .= '</div>';
echo $output;
}
// Optional script to have buttons open in a small popup.
jQuery(document).ready(function($){
$('.bne-share .btn-share.popup').on("click", function() {
if(!window.open( $(this).attr("href"), "", "height=320, width=640, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no" ) ) {
document.location.href = $(this).attr("href")
}
return false;
});
}); // End
/* == Share Buttons CSS == */
.bne-share {
margin: 0;
padding: 0;
display: block;
position: relative;
}
.bne-share li {
display: block;
float: left;
list-style: none;
margin: 0 1px;
}
.btn-share {
border: 2px solid #f2f2f2;
border-color: rgba(220, 220, 220, .4);
border-radius: 2px;
color: #999;
font-size: 18px;
line-height: 40px;
padding: 10px;
width: 40px;
height: 40px;
-webkit-transition: all .25s ease-out;
-moz-transition: all .25s ease-out;
transition: all .25s ease-out;
}
.btn-share:hover, .btn-share:focus {
background: #ddd;
border-color: rgba(0, 0, 0, .1);
color: #fff;
}
.btn-share.facebook:hover, .btn-share.facebook:focus { background: #3b5998; }
.btn-share.twitter:hover, .btn-share.twitter:focus { background: #00aced; }
.btn-share.google:hover, .btn-share.google:focus { background: #dd4b39; }
.btn-share.tumblr:hover, .btn-share.tumblr:focus { background: #32506d; }
.btn-share.pinterest:hover, .btn-share.pinterest:focus { background: #cb2027; }
.btn-share.email:hover, .btn-share.email:focus { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment