Author
FWD:labs
Requirements
WordPress 4.9+
Version (Last Updated)
1.00 (2017-11-17 09:10:08)
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(); }
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"," …"); } $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; }
Questions, comments, concerns or ideas about this? Please let us know.