Last active
April 24, 2018 11:48
-
-
Save Netzberufler/a654074f3a9bbe2440d8cdb602719dd5 to your computer and use it in GitHub Desktop.
WordPress Social Sharing Links
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
/** | |
* Return Social Sharing Links. | |
*/ | |
function theme_slug_social_sharing() { | |
// Get current page URL. | |
$page_url = get_permalink(); | |
// Get current page title. | |
$page_title = get_the_title(); | |
// Create Array with Social Sharing links. | |
$links = array( | |
'facebook' => array( | |
'url' => 'https://www.facebook.com/sharer/sharer.php?u=' . $page_url . '&t=' . $page_title, | |
'text' => 'Facebook', | |
), | |
'twitter' => array( | |
'url' => 'https://twitter.com/intent/tweet?text=' . $page_title . '&url=' .$page_url, | |
'text' => 'Twitter', | |
), | |
'googleplus' => array( | |
'url' => 'https://plus.google.com/share?url=' . $page_url . '&t=' .$page_title, | |
'text' => 'Google+', | |
) | |
); | |
// Create HTML list with Social Sharing links. | |
$html = '<ul class="social-sharing-links">'; | |
foreach( $links as $key => $link ) { | |
$html .= sprintf( '<li><a href="%1$s" target="_blank">%2$s</a></li>', | |
esc_url( $link['url'] ), | |
esc_html( $link['text'] ) | |
); | |
} | |
$html .= '</ul>'; | |
return $html; | |
} |
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
/** | |
* Add the social sharing buttons to post content | |
* | |
* @return string | |
*/ | |
function theme_slug_add_social_sharing_to_content( $content ) { | |
// Only display sharing links in single post and pages. | |
if ( ! ( is_singular() && is_main_query() ) ) { | |
return $content; | |
} | |
return $content . theme_slug_social_sharing(); | |
} | |
add_filter( 'the_content', 'theme_slug_add_social_sharing_to_content' ); |
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
ul.social-sharing-links { | |
display: flex; | |
padding: 0; | |
list-style: none; | |
} | |
ul.social-sharing-links li a { | |
display: block; | |
margin-right: 0.5em; | |
padding: 0.25em 1em; | |
border-radius: 5px; | |
color: #fff; | |
} | |
ul.social-sharing-links li a[href*="facebook.com"] { | |
background: #3b5998; | |
} | |
ul.social-sharing-links li a[href*="twitter.com"] { | |
background: #55acee; | |
} | |
ul.social-sharing-links li a[href*="plus.google.com"] { | |
background: #dc4e41; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment