Skip to content

Instantly share code, notes, and snippets.

@cristianstan
Created March 11, 2022 13:25
Show Gist options
  • Save cristianstan/cbbb6264bf15f0bb3a08f703c62d35d0 to your computer and use it in GitHub Desktop.
Save cristianstan/cbbb6264bf15f0bb3a08f703c62d35d0 to your computer and use it in GitHub Desktop.
Social Share buttons in ModelTheme themes
<?php
// Function to handle the thumbnail request
function angro_get_the_post_thumbnail_src($img)
{
return (preg_match('~\bsrc="([^"]++)"~', $img, $matches)) ? $matches[1] : '';
}
if ( class_exists( 'ReduxFrameworkPlugin' ) ) {
if (!function_exists('angro_social_share_buttons')) {
function angro_social_share_buttons() {
// Get current page URL
$mt_url = esc_url(get_permalink());
// Get current page title
$mt_title = str_replace( ' ', '%20', get_the_title());
// Get Post Thumbnail for pinterest
$mt_thumb = angro_get_the_post_thumbnail_src(get_the_post_thumbnail());
$mt_thumb_url = '';
if(!empty($mt_thumb)) {
$mt_thumb_url = $mt_thumb[0];
}
// Construct sharing URL without using any script
$twitter_url = 'https://twitter.com/intent/tweet?text='.esc_html($mt_title).'&amp;url='.esc_url($mt_url).'&amp;via='.esc_attr(get_bloginfo( 'name' ));
$facebook_url = 'https://www.facebook.com/sharer/sharer.php?u='.esc_url($mt_url);
$whatsapp_url = 'https://api.whatsapp.com/send?text='.esc_html($mt_title) . ' ' . esc_url($mt_url);
$linkedin_url = 'https://www.linkedin.com/shareArticle?mini=true&url='.esc_url($mt_url).'&amp;title='.esc_html($mt_title);
if(!empty($mt_thumb)) {
$pinterest_url = 'https://pinterest.com/pin/create/button/?url='.esc_url($mt_url).'&amp;media='.esc_url($mt_thumb_url).'&amp;description='.esc_html($mt_title);
}else {
$pinterest_url = 'https://pinterest.com/pin/create/button/?url='.esc_url($mt_url).'&amp;description='.esc_html($mt_title);
}
// Based on popular demand added Pinterest too
$pinterest_url = 'https://pinterest.com/pin/create/button/?url='.esc_url($mt_url).'&amp;media='.esc_url($mt_thumb_url).'&amp;description='.esc_html($mt_title);
$email_url = 'mailto:?subject='.esc_html($mt_title).'&amp;body='.esc_url($mt_url);
$telegram_url = 'https://telegram.me/share/url?url=<'.esc_url($mt_url).'>&text=<'.esc_html($mt_title).'>';
$social_shares = angro_redux('angro_social_share_links');
if ($social_shares) {
// The Visual Buttons
echo '<div class="social-box"><div class="social-btn">';
if ($social_shares['twitter'] == 1) {
echo '<a class="col-2 sbtn s-twitter" href="'. esc_url($twitter_url) .'" target="_blank" rel="nofollow"></a>';
}
if ($social_shares['facebook'] == 1) {
echo '<a class="col-2 sbtn s-facebook" href="'.esc_url($facebook_url).'" target="_blank" rel="nofollow"></a>';
}
if ($social_shares['whatsapp'] == 1) {
echo '<a class="col-2 sbtn s-whatsapp" href="'.esc_url($whatsapp_url).'" target="_blank" rel="nofollow"></a>';
}
if ($social_shares['pinterest'] == 1) {
echo '<a class="col-2 sbtn s-pinterest" href="'.esc_url($pinterest_url).'" data-pin-custom="true" target="_blank" rel="nofollow"></a>';
}
if ($social_shares['linkedin'] == 1) {
echo '<a class="col-2 sbtn s-linkedin" href="'.esc_url($linkedin_url).'" target="_blank" rel="nofollow"></a>';
}
if ($social_shares['telegram'] == 1) {
echo '<a class="col-2 sbtn s-telegram" href="'.esc_url($telegram_url).'" target="_blank" rel="nofollow"></a>';
}
if ($social_shares['email'] == 1) {
echo '<a class="col-2 sbtn s-email" href="'.esc_url($email_url).'" target="_blank" rel="nofollow"></a>';
}
echo '</div></div>';
}
}
$social_share_locations = angro_redux('angro_social_share_locations');
if ($social_share_locations['product'] == 1) {
// single product page
add_action( 'woocommerce_product_meta_end', 'angro_social_share_buttons');
}
if ($social_share_locations['post'] == 1) {
// single post page
add_action( 'angro_after_single_post_metas', 'angro_social_share_buttons');
}
}
}
<?php
// Subtab in Social Media tab
# Section: Social Shares
$this->sections[] = array(
'subsection' => true,
'icon' => 'el-icon-share',
'title' => __('Social Shares', 'angro'),
'fields' => array(
array(
'id' => 'angro_social_share_links',
'type' => 'checkbox',
'title' => __('Social Shares', 'angro'),
'subtitle' => __('Choose what social share links to be listed (product pages & posts)', 'angro'),
'options' => array(
'twitter' => __( 'Twitter', 'angro' ),
'facebook' => __( 'Facebook', 'angro' ),
'whatsapp' => __( 'Whatsapp', 'angro' ),
'pinterest' => __( 'Pinterest', 'angro' ),
'linkedin' => __( 'LinkedIn', 'angro' ),
'telegram' => __( 'Telegram', 'angro' ),
'email' => __( 'Email', 'angro' ),
),
'default' => array(
'twitter' => '1',
'facebook' => '1',
'whatsapp' => '1',
'pinterest' => '1',
'linkedin' => '1',
'telegram' => '1',
'email' => '1',
)
),
array(
'id' => 'angro_social_share_locations',
'type' => 'checkbox',
'title' => __('Social Shares Locations', 'angro'),
'subtitle' => __('Enable or disable social share links on product pages or posts', 'angro'),
'options' => array(
'product' => __( 'Product Single', 'angro' ),
'post' => __( 'Post Single', 'angro' ),
),
'default' => array(
'product' => '1',
'post' => '1',
)
),
)
);
/*Social Sharing*/
.social-box {
display: block;
margin: 0;
}
.social-box:last-of-type {
margin: 15px 0;
}
.social-btn {
display: block;
width: 100%;
}
a.col-2.sbtn span {
display: none;
}
a.col-2.sbtn {
width: 6%;
display: inline-block;
text-align: center;
border-radius: 50px;
padding: 5px;
color: #fff;
margin: 0 5px 0 0;
line-height: 1.2;
max-width: 40px;
min-width: 40px;
}
/*twitter*/
.s-twitter {
background: #03A9F4;
}
.s-twitter::before {
font-family: fontawesome;
content: '\f099';
}
.s-twitter:hover {
background: #0093d6;
}
/*facebook*/
.s-facebook {
background: #3F51B5;
}
.s-facebook::before {
font-family: fontawesome;
content: '\f09a';
}
a.col-1.sbtn.s-facebook:hover {
background: #2f409f;
}
/*email*/
.s-email {
background: #d44f42;
}
.s-email::before {
font-family: fontawesome;
content: '\f003';
}
a.col-1.sbtn.s-email:hover {
background: #cb3c2e;
}
/*s-whatsapp*/
.s-whatsapp {
background: #4CAF50;
}
.s-whatsapp::before {
font-family: fontawesome;
content: '\f232';
}
a.col-2.sbtn.s-whatsapp:hover {
background: #3d9440;
}
/*s-linkedin*/
.s-linkedin {
background: #1a7baa;
}
.s-linkedin::before {
font-family: fontawesome;
content: '\f0e1';
}
a.col-2.sbtn.s-linkedin:hover {
background: #136288;
}
/*s-pinterest*/
.s-pinterest {
background: #bd081c;
}
.s-pinterest::before {
font-family: fontawesome;
content: '\f231';
}
a.col-2.sbtn.s-pinterest:hover {
background: #a10718;
}
/*s-telegram*/
.s-telegram {
background: #32AFED;
}
.s-telegram::before {
font-family: fontawesome;
content: '\f2c6';
}
a.col-2.sbtn.s-telegram:hover {
background: #1296d7;
}
.social-btn a:last-of-type {
margin: 0;
}
.single-post .social-box {
float: right;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment