Skip to content

Instantly share code, notes, and snippets.

@patric-boehner
Last active December 5, 2022 23:29
Show Gist options
  • Save patric-boehner/e59e7b2c84c195bfafe21fe0d4a34b1d to your computer and use it in GitHub Desktop.
Save patric-boehner/e59e7b2c84c195bfafe21fe0d4a34b1d to your computer and use it in GitHub Desktop.
Add Simple Custom Social Share Buttons for Genesis
//* This is completly optional. You can leave it off to save some page weight.
/*
* Add to theme/js/dev
* Output to theme/js/social-share.min.js
*/
//* Simpel Social Share Link Popouts
//* http://www.jaredatchison.com/code/create-manual-social-media-share-buttons/
jQuery(document).ready(function($){
$(".share-button").click(function(event){
event.preventDefault();
var window_size = "";
var url = this.href;
var domain = url.split("/")[2];
switch(domain) {
case "www.facebook.com":
window_size = "width=585,height=368";
break;
case "www.twitter.com":
window_size = "width=585,height=261";
break;
case "plus.google.com":
window_size = "width=517,height=511";
break;
case "www.pinterest.com":
window_size = "width=700,height=300";
break;
case "www.reddit.com":
window_size = "width=800,height=400";
break;
default:
window_size = "width=585,height=511";
}
window.open(url, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,' + window_size);
});
});
<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.
/*
* Add to theme/inc/partials
*/
//**********************************************
//* Get Images
//**********************************************
add_action( 'genesis_entry_footer', 'pb_social_share', 10 );
//* https://css-tricks.com/snippets/wordpress/get-the-first-image-from-a-post/
function pb_find_image_url() {
global $post, $posts;
// If a featured image is set first, use that
$first_img = urlencode( genesis_get_image( array( 'format' => 'url' ) ) );
ob_start();
ob_end_clean();
// If no featured image exists, use the first image in the post
if($output = preg_match_all('/<img.*?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches))
{
$first_img = $matches[1][0];
}
// If the post has no images, returen the default image (site logo)
if(empty($first_img)) {
$first_img = urlencode( get_stylesheet_directory_uri() . '/images/logo.png' );
}
// Return the darn image
return $first_img;
}
//**********************************************
//* Social Share Icons
//**********************************************
//* http://www.jaredatchison.com/code/create-manual-social-media-share-buttons/
// Much cleaner than my original version
function pb_social_share() {
//* Variable Setup
$url = urlencode( get_permalink() );
$url_short = urlencode( wp_get_shortlink() );
$title = urlencode( get_the_title() );
$name = urlencode( get_bloginfo( 'name' ) );
$twitter = '@Soemthing';
// Start strucutre
echo '<div class="social-share">';
//* Facebook
echo '<a href="http://www.facebook.com/sharer/sharer.php?u=' . $url . '&t=' . $title .'" class="share-button icon-facebook" title="Share on Facebook" target="_blank"></a>';
//* Twitter
echo '<a href="http://www.twitter.com/intent/tweet?url=' . $url_short . '&text=' .$twitter. '&nbsp;' . $title . '" class="share-button icon-twitter" title="Tweet This Post" target="_blank"></a>';
//* Pinterest
echo '<a href="http://pinterest.com/pin/create/button/?url=' . $url . '&media=' . pb_find_image_url() . '&nbsp;&description=' . $title . '" class="share-button icon-pinterest" title="Share On Pinterest" target="_blank"></a>';
//* Mail
echo '<a href="mailto:?subject='.$name.'&body=' .$name. '&nbsp;blog&nbsp;post:&nbsp;' .$title. '&nbsp;&ndash;&nbsp;' .$url. '" class="share-button icon-envelope" title="Email This Post" target="_blank"></a> ';
// End of Structure
echo '</div>';
echo '<!--End Social Share-->';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment