Open Source Code

WordPress Social Media Share/Follow

Overview

Author
FWD:labs

Requirements
WordPress 4.9+

Version (Last Updated)
1.00 (2017-11-17 09:10:08)

License

The MIT License (MIT)

Copyright (c) 2016 FWD:labs

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Instructions

1. Edit config to add/edit/remove social media networks.
2. Add this code to your WordPress theme's functions.php file.
3. Add to your template: if (function_exists('fwdlabs_socialmedia')) { echo fwdlabs_socialmedia(); }

Code

function fwdlabs_socialmedia() {

	global $post;

	// START CONFIG
	
	// Use one of the examples below to either replicate or replace which social share links/buttons are included
	$shareNetworks = array();
	
	// We're defining a button (text or image) and then a share link
	// For the button, you can swap the text with HTML -- e.g. <img src=""> -- to use an icon instead
	// Not included in this function, if you use an image instead of text, you may want to load up the template directory -- e.g. get_bloginfo('template_directory') -- to store your icons
	
	// CONFIG - FACEBOOK
	$shareNetworks['facebook']['button'] = 'Facebook';
	$shareNetworks['facebook']['link'] = '<a href="http://www.facebook.com/sharer.php?u='.$shareURL.'&t='.$shareTitle.'&src=sp">'.$shareNetworks['facebook']['button'].'</a>'; // Swap this with your Facebook home page if you prefer "follow" over "share" links

	// CONFIG - TWITTER
	$shareNetworks['twitter']['button'] = 'Twitter';
	$shareNetworks['twitter']['link'] = '<a href="https://twitter.com/intent/tweet?original_referer='.$shareURL.'&source=tweetbutton&text='.htmlspecialchars($shareTitle).'&url='.$shareURL.'">'.$shareNetworks['twitter']['button'].'</a>';
	
	// STOP CONFIG
	
	// PERMALINK
	// Ensure a canonical permalink, relying on WordPress when possible
	$shareURL = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'];
	if (is_single() || is_page()) {
		$shareURL = get_permalink(); // This function creates a full URL
	} else if (is_category()) {
		$get_category = get_category($cat);
		if ($get_category) {
			$shareURL .= "/".$get_category->category_nicename."/";
		}
	}

	// TITLE
	if (is_single() || is_page()) {
		$shareTitle = str_replace('"',"'",get_the_title())." - ".get_bloginfo('name');
	} else if (is_category()) {
		$shareTitle = single_cat_title('', false);
	} else {
		$shareTitle = get_bloginfo('name');
	}
	$shareTitle = strip_tags($shareTitle);

	// DESCRIPTION
	if (is_single() || is_page()) {
		$excerpt = trim(htmlspecialchars(strip_tags(strip_shortcodes($post->post_content))));
		// Not included in this function, you may have a separate truncate function to keep this text to a specific character count
		if (function_exists('truncate')) {
			$excerpt = truncate($excerpt,"250"," &hellip;");
		}
		$shareDescription = $excerpt;
	} else {
		$shareDescription = get_bloginfo('description');
	}

	// THUMBNAIL
	$shareThumbnail = "";
	if (is_single() || is_page()) {
		// If there's a Custom Field with a key named "cover" and a value that's a full URL, this gives preference to a more unique share thumbnail
		// Not included in this function, one alternative here is to use a page/post "featured image"
		$getShareThumbnail = get_post_meta($post->ID, "cover", true);
		if ($getShareThumbnail) {
			$shareThumbnail = $getShareThumbnail;
		}
	}
	
	// BUILD HTML
	$display = '<ul id="share">'."\n";
	foreach ($shareNetworks as $shareNetwork) {
		$display .= "<li>".$shareNetwork['link']."</li>\n";
	}
	$display .= "</ul>\n";

	return $display;

}

Changelog

Feedback

Questions, comments, concerns or ideas about this? Please let us know.

Twitter   Facebook   Instagram