Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:16
-
Save bearded-avenger/5d7eb52f1b27013ec95d to your computer and use it in GitHub Desktop.
dead-simple-social-shit-4000-super-duper-beards-with-goats.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* This class holds all teh functionality for social sharing and social data so you dont need a plugin | |
* Ex: https://lasso.is/now-entering-the-arena/ (bottom) | |
* | |
* use social sharing like so: | |
* <a href="#" class="post-share__twitter">share twitter</a> | |
* <a href="#" class="post-share__fb">share fb</a> | |
*/ | |
class myThemeSocial { | |
function __construct(){ | |
// social sharing | |
add_action('wp_head', array($this,'social_data')); | |
add_action('wp_head', array($this,'story_sharing')); | |
add_action('wp_footer', array($this,'vendor_sdk')); | |
} | |
/** | |
* | |
* Load Facebook and Twitter Open Graph and Meta Card Stuffs | |
*/ | |
function social_data(){ | |
$author = get_post_field( 'post_author', get_the_ID() ); | |
$tw_hndl = 'aesopinteractiv'; | |
$twitter_owner = $tw_hndl ? sprintf('<meta name="twitter:creator" content="@%s"> | |
<meta name="twitter:site" content="@%s">', | |
esc_attr( trim( $tw_hndl ) ), | |
esc_attr( trim( $tw_hndl ) ) | |
) : false; | |
?> | |
<!-- Facebook Open Graph --> | |
<meta property="og:url" content="<?php echo esc_url( get_permalink() );?>"> | |
<?php if ( is_singular() ) { ?> | |
<meta property="og:title" content="<?php echo esc_attr( get_the_title() );?>"> | |
<?php } else { ?> | |
<meta property="og:title" content="<?php echo esc_attr( get_bloginfo('name') );?>"> | |
<?php } ?> | |
<meta property="og:description" content="<?php echo esc_attr( self::excerpt() );?>"> | |
<meta property="og:site_name" content="<?php echo esc_attr( get_bloginfo('name') );?>"> | |
<meta property="og:type" content="website"> | |
<?php echo !is_404() ? self::get_img('property','og', 'fb') : false;?> | |
<meta property="og:locale" content="en_us"> | |
<!-- Twitter Card Meta --> | |
<meta name="twitter:card" content="summary"> | |
<?php echo trim( $twitter_owner );?> | |
<meta name="twitter:title" content="<?php echo esc_attr( get_the_title() );?>"> | |
<meta name="twitter:description" content="<?php echo self::excerpt();?>"> | |
<meta name="twitter:domain" content="<?php echo get_permalink();?>"> | |
<?php echo !is_404() ? self::get_img('name','twitter', 'twitter') : false;?> | |
<?php | |
} | |
/** | |
* | |
* Main fucntion responsible for rich sharing to Facebook and Twitter | |
* | |
* | |
*/ | |
function story_sharing(){ | |
if ( is_single() ): | |
$postid = get_the_ID(); | |
$author = get_post_field( 'post_author', $postid ); | |
$tw_hndl = 'aesopinteractiv'; | |
$tw_hndl = !empty( $tw_hndl ) ? sprintf('by @%s', esc_attr( trim( $tw_hndl ) ) ) : false; | |
$text = sprintf('\'%s\' %s &url=%s', trim( get_the_title() ), $tw_hndl, get_permalink() ); | |
?> | |
<script> | |
function fbAsyncInit() { | |
FB.init({ | |
appId : 'YOUR_FACEBOOK_APP_ID', | |
xfbml : true, | |
version : 'v2.2' | |
}); | |
jQuery('.post-share__fb').click(function(e){ | |
e.preventDefault(); | |
FB.ui({ | |
method: 'share', | |
href: '<?php esc_url( the_permalink() );?>' | |
}); | |
}); | |
} | |
jQuery(document).ready(function($){ | |
$('.post-share__twitter').attr('href', "https://twitter.com/intent/tweet?text=<?php echo $text;?>"); | |
}); | |
</script> | |
<?php | |
endif; | |
} | |
/** | |
* | |
* Load external sdks for social sharing | |
* | |
* | |
*/ | |
function vendor_sdk(){ | |
if ( is_single() ): | |
?> | |
<div id="fb-root"></div> | |
<script async="" src="//platform.twitter.com/widgets.js"></script> | |
<script async="" src="//connect.facebook.net/en_US/all.js"></script> | |
<?php | |
endif; | |
} | |
/** | |
* | |
* Get the image attached to a post and return the data for either facebook or twitter | |
* @todo - need to set a default if no post image?? | |
* @return post thumbnail | |
*/ | |
public static function get_img($name = '',$prop = '', $vendor = ''){ | |
$imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full'); | |
return has_post_thumbnail() ? sprintf('<meta %s="%s:image" content="%s">', $name, $prop, $imgsrc[0]) : false; | |
} | |
/** | |
* | |
* If we're on the home page return the sites desription | |
* otherwise return the excerpt | |
* | |
* @return an excerpt if on single, otherwise site description | |
*/ | |
function excerpt(){ | |
global $post; | |
if ( isset( $post ) ) { | |
$out = wp_trim_words( esc_attr( strip_tags( stripslashes( $post->post_content ) ) ) , 25, '...' ); | |
} else { | |
$out = get_bloginfo('description'); | |
} | |
return $out; | |
} | |
} | |
new myThemeSocial; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment