Created
January 17, 2018 21:02
-
-
Save aosipov/4f6526ab0124f995ef2fa2432380270b to your computer and use it in GitHub Desktop.
Social sharing with Pinterest thumbnail
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
// Social sharing | |
function social_sharing_buttons($content) { | |
global $post; | |
if(is_singular() || is_home()){ | |
// Get current page URL | |
$crunchifyURL = urlencode(get_permalink()); | |
// Get current page title | |
$crunchifyTitle = str_replace( ' ', '%20', get_the_title()); | |
// Get Post Thumbnail for pinterest | |
$crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); | |
// Sharing URL | |
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL; | |
$googleURL = 'https://plus.google.com/share?url='.$crunchifyURL; | |
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$crunchifyURL.'&media='.$crunchifyThumbnail[0].'&description='.$crunchifyTitle; | |
// Add sharing button at the end of page/page content | |
$content .= '<!-- Social sharing -->'; | |
$content .= '<div class="social-block">'; | |
$content .= '<a class="share-facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>'; | |
$content .= '<a class="share-pinterest" href="'.$pinterestURL.'" data-pin-custom="true" target="_blank">Pin It</a>'; | |
$content .= '</div>'; | |
return $content; | |
}else{ | |
// if not a post/page then don't include sharing button | |
return $content; | |
} | |
}; | |
add_filter( 'the_content', 'social_sharing_buttons'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment