From ea7dcc1c89ed779bcaf913f0da5202b1ef3a83cf Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 25 Oct 2018 11:07:13 +0200 Subject: [PATCH] functions/social: use crunchify social sharing buttons as a base Source: https://crunchify.com/how-to-create-social-sharing-button-without-any-plugin-and-script-loading-wordpress-speed-optimization-goal/ --- functions.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/functions.php b/functions.php index a6ebca1..7414567 100644 --- a/functions.php +++ b/functions.php @@ -48,3 +48,50 @@ function wpqdouze_post_supports() { } add_action( 'init', 'wpqdouze_post_supports' ); + +/* 4. Social Network */ + +function crunchify_social_sharing_buttons($content) { + global $post; + if(is_singular() || is_home()){ + + // Get current page URL + $crunchifyURL = urlencode(get_permalink()); + + // Get current page title + $crunchifyTitle = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8'); + // $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' ); + + // Construct sharing URL without using any script + $twitterURL = 'https://twitter.com/intent/tweet?text='.$crunchifyTitle.'&url='.$crunchifyURL.'&via=Crunchify'; + $facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL; + $googleURL = 'https://plus.google.com/share?url='.$crunchifyURL; + $bufferURL = 'https://bufferapp.com/add?url='.$crunchifyURL.'&text='.$crunchifyTitle; + $whatsappURL = 'whatsapp://send?text='.$crunchifyTitle . ' ' . $crunchifyURL; + $linkedInURL = 'https://www.linkedin.com/shareArticle?mini=true&url='.$crunchifyURL.'&title='.$crunchifyTitle; + + // Based on popular demand added Pinterest too + $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 .= ''; + $content .= '
'; + $content .= '
SHARE ON
Twitter'; + $content .= 'Facebook'; + $content .= 'WhatsApp'; + $content .= 'Google+'; + $content .= 'Buffer'; + $content .= 'LinkedIn'; + $content .= 'Pin It'; + $content .= '
'; + + return $content; + }else{ + // if not a post/page then don't include sharing button + return $content; + } +}; +add_filter( 'the_content', 'crunchify_social_sharing_buttons');