Skip to content

Instantly share code, notes, and snippets.

@mklasen
Last active October 23, 2019 11:11
Show Gist options
  • Save mklasen/a3f4803cb8cd68efb839ea406606c9c7 to your computer and use it in GitHub Desktop.
Save mklasen/a3f4803cb8cd68efb839ea406606c9c7 to your computer and use it in GitHub Desktop.
Snippet for displaying social media buttons without using external libraries
<?php
public function social_media_buttons() {
$output = '';
$enabled_platforms = array(
'googleplus',
'facebook',
'twitter',
'print',
);
$id = get_the_ID();
$title = get_the_title( $id );
$url = get_permalink( $id );
$excerpt = get_the_excerpt( $id );
$site_url = get_home_url();
$image_id = get_field( 'main_image', $id );
$image = wp_get_attachment_image_src( $image_id, 'full' );
$image_src = $image[0];
$image_width = $image[1];
$image_height = $image[2];
$platforms = array(
'twitter' => 'http://twitter.com/intent/tweet?status=' . $title . '+' . $url . '',
'pinterest' => 'http://pinterest.com/pin/create/bookmarklet/?media=' . $image_src . '&url=' . $url . '&is_video=false&description=' . $title . '',
'facebook' => 'http://www.facebook.com/sharer/sharer.php?u=' . $url . '&title=' . $title . '',
'googleplus' => 'https://plus.google.com/share?url=' . $url . '',
'reddit' => 'http://www.reddit.com/submit?url=' . $url . '&title=' . $title . '',
'delicious' => 'http://del.icio.us/post?url=' . $url . '&title=' . $title . '&notes=' . $excerpt . '',
'digg' => 'https://digg.com/submit?url=' . $url . '&title=' . $title . '',
'tapiture' => 'http://tapiture.com/bookmarklet/image?img_src=' . $image_src . '&page_url=' . $url . '&page_title=' . $title . '&img_title=' . $title . '&img_width=' . $image_width . 'img_height=' . $image_height . '',
'stumbleupon' => 'http://www.stumbleupon.com/submit?url=' . $url . '&title=' . $title . '',
'linkedin' => 'http://www.linkedin.com/shareArticle?mini=true&url=' . $url . '&title=' . $title . '&source=' . $site_url . '',
'slashdot' => 'http://slashdot.org/bookmark.pl?url=' . $url . '&title=' . $title . '',
'technorati' => 'http://technorati.com/faves?add=' . $url . '&title=' . $title . '',
'posterous' => 'http://posterous.com/share?linkto=' . $url . '',
'tumblr' => 'http://www.tumblr.com/share?v=3&u=' . $url . '&t=' . $title . '',
'googlebookmarks' => 'http://www.google.com/bookmarks/mark?op=edit&bkmk=' . $url . '&title=' . $title . '&annotation=' . $excerpt . '',
'newsvine' => 'http://www.newsvine.com/_tools/seed&save?u=' . $url . '&h=' . $title . '',
'pingfm' => 'http://ping.fm/ref/?link=' . $url . '&title=' . $title . '&body=' . $excerpt . '',
'evernote' => 'http://www.evernote.com/clip.action?url=' . $url . '&title=' . $title . '',
'friendfeed' => 'http://www.friendfeed.com/share?url=' . $url . '&title=' . $title . '',
'print' => 'javascript:print()',
);
if ( ! empty( $enabled_platforms ) ) {
$output .= '<ul>';
foreach ( $platforms as $key => $platform ) {
if ( in_array( $key, $enabled_platforms ) ) {
$output .= '<li class="social-media-link link-' . $key . '"><a href="' . ($key !== 'print' ? esc_url( $platform ) : $platform) . '"></a></li>';
}
}
$output .= '</ul>';
}
return $output;
}
<?php
$platforms = array(
'twitter' => [
'link' => 'http://twitter.com/intent/tweet?status=' . $title . '+' . $url . '',
'html' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="0" fill="none" width="24" height="24"/><g><path d="M19 3H5c-1.105 0-2 .895-2 2v14c0 1.105.895 2 2 2h14c1.105 0 2-.895 2-2V5c0-1.105-.895-2-2-2zm-2.534 6.71c.004.1.007.198.007.298 0 3.045-2.318 6.556-6.556 6.556-1.3 0-2.512-.38-3.532-1.035.18.02.364.03.55.03 1.08 0 2.073-.367 2.862-.985-1.008-.02-1.86-.685-2.152-1.6.14.027.285.04.433.04.21 0 .414-.027.607-.08-1.054-.212-1.848-1.143-1.848-2.26v-.028c.31.173.666.276 1.044.288-.617-.413-1.024-1.118-1.024-1.918 0-.422.114-.818.312-1.158 1.136 1.393 2.834 2.31 4.75 2.406-.04-.17-.06-.344-.06-.525 0-1.27 1.03-2.303 2.303-2.303.664 0 1.262.28 1.683.728.525-.103 1.018-.295 1.463-.56-.172.54-.537.99-1.013 1.276.466-.055.91-.18 1.323-.362-.31.46-.7.867-1.15 1.192z"/></g></svg>'
],
'pinterest' => [
'link' => 'http://pinterest.com/pin/create/bookmarklet/?media=' . $image_src . '&url=' . $url . '&is_video=false&description=' . $title . '',
'html' => ''
],
'facebook' => [
'link' => 'http://www.facebook.com/sharer/sharer.php?u=' . $url . '&title=' . $title . '',
'html' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="0" fill="none" width="24" height="24"/><g><path d="M12 2C6.5 2 2 6.5 2 12c0 5 3.7 9.1 8.4 9.9v-7H7.9V12h2.5V9.8c0-2.5 1.5-3.9 3.8-3.9 1.1 0 2.2.2 2.2.2v2.5h-1.3c-1.2 0-1.6.8-1.6 1.6V12h2.8l-.4 2.9h-2.3v7C18.3 21.1 22 17 22 12c0-5.5-4.5-10-10-10z"/></g></svg>'
],
'googleplus' => [
'link' => 'https://plus.google.com/share?url=' . $url . '',
'html' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="0" fill="none" width="24" height="24"/><g><path d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm-1.92 14.05c-2.235 0-4.05-1.814-4.05-4.05s1.815-4.05 4.05-4.05c1.095 0 2.01.4 2.71 1.057l-1.15 1.118c-.292-.275-.802-.6-1.56-.6-1.34 0-2.433 1.115-2.433 2.48s1.094 2.48 2.434 2.48c1.552 0 2.123-1.074 2.228-1.71h-2.232v-1.51h3.79c.058.255.102.494.102.83 0 2.312-1.55 3.956-3.887 3.956zm8.92-3.3h-1.25V14h-1.5v-1.25H15v-1.5h1.25V10h1.5v1.25H19v1.5z"/></g></svg>'
],
'reddit' => [
'link' => 'http://www.reddit.com/submit?url=' . $url . '&title=' . $title . '',
'html' => ''
],
'delicious' => [
'link' => 'http://del.icio.us/post?url=' . $url . '&title=' . $title . '&notes=' . $excerpt . '',
'html' => ''
],
'digg' => [
'link' => 'https://digg.com/submit?url=' . $url . '&title=' . $title . '',
'html' => ''
],
'tapiture' => [
'link' => 'http://tapiture.com/bookmarklet/image?img_src=' . $image_src . '&page_url=' . $url . '&page_title=' . $title . '&img_title=' . $title . '&img_width=' . $image_width . 'img_height=' . $image_height . '',
'html' => ''
],
'stumbleupon' => [
'link' => 'http://www.stumbleupon.com/submit?url=' . $url . '&title=' . $title . '',
'html' => ''
],
'linkedin' => [
'link' => 'http://www.linkedin.com/shareArticle?mini=true&url=' . $url . '&title=' . $title . '&source=' . $site_url . '',
'html' => ''
],
'slashdot' => [
'link' => 'http://slashdot.org/bookmark.pl?url=' . $url . '&title=' . $title . '',
'html' => ''
],
'technorati' => [
'link' => 'http://technorati.com/faves?add=' . $url . '&title=' . $title . '',
'html' => ''
],
'posterous' => [
'link' => 'http://posterous.com/share?linkto=' . $url . '',
'html' => ''
],
'tumblr' => [
'link' => 'http://www.tumblr.com/share?v=3&u=' . $url . '&t=' . $title . '',
'html' => ''
],
'googlebookmarks' => [
'link' => 'http://www.google.com/bookmarks/mark?op=edit&bkmk=' . $url . '&title=' . $title . '&annotation=' . $excerpt . '',
'html' => ''
],
'newsvine' => [
'link' => 'http://www.newsvine.com/_tools/seed&save?u=' . $url . '&h=' . $title . '',
'html' => ''
],
'pingfm' => [
'link' => 'http://ping.fm/ref/?link=' . $url . '&title=' . $title . '&body=' . $excerpt . '',
'html' => ''
],
'evernote' => [
'link' => 'http://www.evernote.com/clip.action?url=' . $url . '&title=' . $title . '',
'html' => ''
],
'friendfeed' => [
'link' => 'http://www.friendfeed.com/share?url=' . $url . '&title=' . $title . '',
'html' => ''
],
'print' => [
'link' => 'javascript:print()',
'html' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="0" fill="none" width="24" height="24"/><g><path d="M9 16h6v2H9v-2zm13 1h-3v3c0 1.105-.895 2-2 2H7c-1.105 0-2-.895-2-2v-3H2V9c0-1.105.895-2 2-2h1V5c0-1.105.895-2 2-2h10c1.105 0 2 .895 2 2v2h1c1.105 0 2 .895 2 2v8zM7 7h10V5H7v2zm10 7H7v6h10v-6zm3-3.5c0-.828-.672-1.5-1.5-1.5s-1.5.672-1.5 1.5.672 1.5 1.5 1.5 1.5-.672 1.5-1.5z"/></g></svg>'
],
'email' => [
'link' => 'mailto:&subject=' . get_the_title() . '&body=' . get_the_excerpt(),
'html' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="0" fill="none" width="24" height="24"/><g><path d="M20 4H4c-1.105 0-2 .895-2 2v12c0 1.105.895 2 2 2h16c1.105 0 2-.895 2-2V6c0-1.105-.895-2-2-2zm0 4.236l-8 4.882-8-4.882V6h16v2.236z"/></g></svg>'
]
);
$output .= '<li class="social-media-link link-' . $key . '"><a href="' . ( $key !== 'print' ? esc_url( $platform['link'] ) : $platform['link'] ) . '">';
if ( isset( $platform['html'] ) && ! empty( $platform['html'] ) ) {
$output .= $platform['html'];
}
$output .= '</a></li>';
@Rubsel
Copy link

Rubsel commented Oct 7, 2019

Is Google+ still needed? Since its not social media :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment