Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Share Links to WordPress Themes with a single function
// copyright Danny Wahl | iyware.com | License: CC-BY-4.0
function shareLinks(array $sites = NULL) {
$share = array();
$share["siteTitle"] = get_bloginfo('name');
$share["siteURL"] = get_site_url();
$share["postTitle"] = get_the_title();
$share["postURL"] = remove_query_arg( 'p', get_permalink());
$share["postCategory"] = get_the_category();
$share["postExcerpt"] = wp_strip_all_tags(get_the_excerpt());
$share["postShortExcerpt"] = substr($share["postExcerpt"], 0, 90);
$share["postImage"] = wp_get_attachment_url(get_post_thumbnail_id());
if(empty($share["postImage"])) { //No Featured image, get first inline image
$args = array(
'numberposts' => 1,
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $postID,
'post_status' => null,
'post_type' => 'attachment',
);
$postAttachments = get_children($args);
if($postAttachments) {
$src = current($postAttachments);
$src = wp_get_attachment_image_src($src->ID, 'full');
$share["postImage"] = $src[0];
}
unset($args);
unset($postAttachments);
}
$hash = '';
$dlbHash = '';
foreach($share["postCategory"] as $value) {
$val = $value->cat_name;
$val = str_replace(' ', '', $val);
$hash .= ' #' . $val . '';
$dblhash .= ' #' . $val . '#';
}
$share["postCategory"] = $hash;
$share["postDblCategory"] = $dblhash;
unset($hash);
unset($dblHash);
$networks = array();
$networks["email"] = [
"name" => "E-mail",
"url" => "mailto:?",
"query" => [
"subject" => $share["siteTitle"] . ": " . $share["postTitle"],
"body" => $share["postTitle"] . "\r\n" . $share["postURL"] . "\r\n\r\n" . $share["postExcerpt"],
],
];
$networks["twitter"] = [
"name" => "twitter",
"url" => "https://twitter.com/home?",
"query" => [
"status" => $share["postURL"] . $share["postCategory"],
],
];
$networks["facebook"] = [
"name" => "Facebook",
"url" => "https://www.facebook.com/sharer/sharer.php?",
"query" => [
"u" => $share["postURL"],
],
];
$networks["googlePlus"] = [
"name" => "Google+",
"url" => "https://plus.google.com/share?",
"query" => [
"url" => $share["postURL"],
],
];
$networks["linkedin"] = [
"name" => "LinkedIn",
"url" => "https://www.linkedin.com/shareArticle?",
"query" => [
"mini" => "true",
"url" => $share["postURL"],
"title" => $share["postTitle"],
"summary" => $share["postExcerpt"],
"source" => $share["siteTitle"],
],
];
$networks["pinterest"] = [
"name" => "Pinterest",
"url" => "https://pinterest.com/pin/create/button/?",
"query" => [
"url" => $share["postURL"],
"media" => $share["postImage"],
"description" => $share["postShortExcerpt"],
],
];
$networks["tumblr"] = [
"name" => "tumblr",
"url" => "http://www.tumblr.com/share/link?",
"query" => [
"url" => $share["postURL"],
"name" => $share["postTitle"],
"description" => $share["postExcerpt"],
],
];
$networks["reddit"] = [
"name" => "reddit",
"url" => "http://www.reddit.com/submit/?",
"query" => [
"url" => $share["postURL"],
"title" => $share["postTitle"],
],
];
$networks["xing"] = [
"name" => "Xing",
"url" => "https://www.xing.com/spi/shares/new?",
"query" => [
"sc_p" => "xing-share",
"url" => $share["postURL"],
],
];
$networks["vk"] = [
"name" => "VK",
"url" => "https://vk.com/share.php?",
"query" => [
"url" => $share["postURL"],
"title" => $share["postTitle"],
"description" => $share["postExcerpt"],
"image" => $share["postImage"],
"noparse" => "true",
],
];
$networks["buffer"] = [
"name" => "buffer",
"url" => "https://bufferapp.com/add/?",
"query" => [
"url" => $share["postURL"],
"text" => $share["postShortExcerpt"],
],
];
$networks["stumbleupon"] = [
"name" => "StumbleUpon",
"url" => "https://www.stumbleupon.com/submit?",
"query" => [
"url" => $share["postURL"],
"title" => $share["postTitle"],
],
];
$networks["sinaWeibo"] = [
"name" => "新浪微博",
"url" => "http://service.weibo.com/share/share.php?",
"query" => [
"url" => $share["postURL"],
"pic" => $share["postImage"],
"title" => $share["postTitle"] . "%0D%0A" . $share["postShortExcerpt"] . $share["postDblCategory"],
],
];
$networks["baidu"] = [
"name" => "Baidu空间",
"url" => "http://hi.baidu.com/pub/show/share?",
"query" => [
"url" => $share["postURL"],
"title" => $share["postTitle"],
],
];
$networks["renren"] = [
"name" => "人人网",
"url" => "http://share.renren.com/share/buttonshare?",
"query" => [
"link" => $share["postURL"],
"title" => $share["postTitle"],
],
];
$networks["tencentWeibo"] = [
"name" => "腾讯微博",
"url" => "http://v.t.qq.com/share/share.php?",
"query" => [
"title" => $share["postTitle"] . " " . $share["postShortExcerpt"] . $share["postDblCategory"],
"url" => $share["postURL"],
],
];
$networks["qzone"] = [
"name" => "QQ空间",
"url" => "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?",
"query" => [
"title" => $share["postTitle"],
"url" => $share["postURL"],
"summary" => $share["postShortExcerpt"],
],
];
$networks["pengyou"] = [
"name" => "腾讯朋友网",
"url" => "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?",
"query" => [
"to" => "pengyou",
"title" => $share["postTitle"],
"url" => $share["postURL"],
"summary" => $share["postShortExcerpt"],
],
];
$output = '';
if(isset($sites)) { // Only output requested sites
foreach($sites as $site) {
if(array_key_exists($site, $networks)){
$output .= '<li><a class="' . $site . '" href="' . $networks[$site]["url"] . http_build_query($networks[$site]["query"], null, "&", PHP_QUERY_RFC3986) . '">' . $networks[$site]["name"] . '</a></li>';
}
}
} else { // Output all the sites!
foreach($networks as $network){
$output .= '<li><a class="' . key($networks) . '" href="' . $network["url"] . http_build_query($network["query"], null, "&", PHP_QUERY_RFC3986) . '">' . $network["name"] . '</a></li>';
}
}
if(!empty($output)) {
$output = '<ul>' . $output . '</ul>';
}
return $output;
}
echo shareLinks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment