Best social network snippets for WordPress
Automatically add Open Graph to your posts
Open Graph is a protocol created by facebook which allow you to make your content easily recognizable by social networks. To automatically add Open Graph metadata to your posts, let’s start by pasting the following code snippet into your functions.php file.
function wptuts_opengraph_for_posts() { if ( is_singular() ) { global $post; setup_postdata( $post ); $output = '<meta property="og:type" content="article" />' . "\n"; $output .= '<meta property="og:title" content="' . esc_attr( get_the_title() ) . '" />' . "\n"; $output .= '<meta property="og:url" content="' . get_permalink() . '" />' . "\n"; $output .= '<meta property="og:description" content="' . esc_attr( get_the_excerpt() ) . '" />' . "\n"; if ( has_post_thumbnail() ) { $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' ); $output .= '<meta property="og:image" content="' . $imgsrc[0] . '" />' . "\n"; } echo $output; } } add_action( 'wp_head', 'wptuts_opengraph_for_posts' );
Then, open your header.php and replace the tag by this:
<html <?php language_attributes(); ?> prefix="og: http://ogp.me/ns#">
Create a Pinterest “pin it” button for your blog
Pinterest is a website where people can create and share bookmarks of interesting stuff they find among the web. Like Facebook or Twitter, a Pinterest button can be added to your posts in order to make it easy for people to add your content to the network.
The first thing to do is to paste the following snippet where you’d like the “Pin It” button to be displayed. Note that this code must be inserted within the loop.
<a href="http://pinterest.com/<wbr>pin/create/button/?url=<?php the_permalink(); ?>&media=<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post-><wbr>ID), 'thumbnail' ); echo $thumb['0']; ?>&description=<?php the_title(); ?>" count-layout="horizontal">Pin It</a></wbr></wbr>
Once done, open your footer.php file and add the Pinterest JavaScript code:
<script type="text/javascript" src="http://assets.pinterest.<wbr>com/js/pinit.js"></script>
Display your latest tweet on your blog without using a plugin
Many plugins are available if you’re looking for a way to display your latest tweet on your blog. But you don’t really need to install a plugin for that. Simply paste the following code anywhere in your theme files, where you want the tweets to be displayed. Don’t forget:
- To update the code with your username on line 3
- Max items to display can be defined on line 4.
<?php include_once(ABSPATH . WPINC . '/feed.php'); $rss = fetch_feed('<a href="https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=techjam_gr'" target="_blank">https://api.<wbr>twitter.com/1/statuses/user_<wbr>timeline.rss?screen_name=<wbr>techjam_gr'</wbr></wbr></wbr></a>); $maxitems = $rss->get_item_quantity(3); $rss_items = $rss->get_items(0, $maxitems); ?> <ul> <?php if ($maxitems == 0) echo '<li>No items.</li>'; else // Loop through each feed item and display each item as a hyperlink. foreach ( $rss_items as $item ) : ?> <li> <a href='<?php echo $item->get_permalink(); ?>'> <?php echo $item->get_title(); ?> </a> </li> <?php endforeach; ?> </ul>
Add a Google+ button to your posts
Google+ is a social network from Google, and it’s becoming more popular day by day. To add a google+ button to your posts, simply open your functions.php file and paste the following code in it:
add_filter('the_content', 'wpr_google_plusone'); function wpr_google_plusone($content) { $content = $content.'<div><g:plusone size="tall" href="'.get_permalink().'"></<wbr>g:plusone></div>'; return $content; } add_action ('wp_enqueue_scripts','wpr_<wbr>google_plusone_script'); function wpr_google_plusone_script() { wp_enqueue_script('google-<wbr>plusone', '<a href="https://apis.google.com/js/plusone.js" target="_blank">https://apis.google.com/js/<wbr>plusone.js</wbr></a>', array(), null); }</wbr></wbr></wbr>
Display your lastest Google+ update
If you’re a Google+ user, you might want to display your latest Google+ update. To do so, paste the code below where you want to display your latest Google+ update. Just don’t forget to put your Google+ ID on line 3.
<?php include_once(ABSPATH.WPINC.'/<wbr>rss.php'); $googleplus = fetch_feed("<a href="http://plusfeed.appspot.com/103329092193061943712" target="_blank">http://plusfeed.<wbr>appspot.com/<wbr>103329092193061943712</wbr></wbr></a>"); // Replace 103329092193061943712 by your own ID echo '<a href="'; echo $googleplus->items[0]['link']; echo '">'; echo $googleplus->items[0]['<wbr>summary']; echo ''; ?></wbr></wbr>
Automatically add Twitter and Facebook buttons to your posts
Facebook and Twitter are the two most popular social networks, and they can provide lots of traffic to your website. In order to make it easy for people to share your posts on Twitter and Facebook, paste this code into your functions.php file and save it. It will display both Facebook and Twitter buttons.
function share_this($content){ if(!is_feed() && !is_home()) { $content .= '<div> <a href="http://twitter.com/share<wbr>" class="twitter-share-button" data-count="horizontal">Tweet<<wbr>/a> <script type="text/javascript" src="http://platform.twitter.<wbr>com/widgets.js"></script> <div> <iframe src="http://www.facebook.com/<wbr>plugins/like.php?href='. urlencode(get_permalink($post-<wbr>>ID)) .'&layout=button_count&<wbr>amp;show_faces=false&<wbr>width=200&action=like&<wbr>colorscheme=light&height=<wbr>21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:200px; height:21px;" allowTransparency="true"></<wbr>iframe> </div> </div>'; } return $content; } add_action('the_content', 'share_this');</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
Display your favorite tweets with WordPress
<?php include_once(ABSPATH . WPINC . '/feed.php'); $rss = fetch_feed('<a href="http://twitter.com/favorites/793830.rss'" target="_blank">http://twitter.<wbr>com/favorites/793830.rss'</wbr></a>); $maxitems = $rss->get_item_quantity(3); $rss_items = $rss->get_items(0, $maxitems); ?> <ul> <?php if ($maxitems == 0) echo '<li>No items.</li>'; else // Loop through each feed item and display each item as a hyperlink. foreach ( $rss_items as $item ) : ?> <li> <a href='<?php echo $item->get_permalink(); ?>'> <?php echo $item->get_title(); ?> </a> </li> <?php endforeach; ?> </ul>
Display how many times a post has been shared on twitter
If you want to display how many times a post has been shared on Twitter, simply paste the function below into your functions.php file:
function readTwitterShares($url) { $s = file_get_contents("<a href="http://urls.api.twitter.com/1/urls/count.json" target="_blank">http://<wbr>urls.api.twitter.com/1/urls/<wbr>count.json</wbr></wbr></a>". "?callback=?&url=".urlencode($<wbr>url)); preg_match("#(\"count\"):([0-<wbr>9]*)#",$s,$ar); return isset($ar[2]) ? $ar[2] : 0; }</wbr></wbr>
Then use the following code within the loop to display how many times the current post has been shared on Twitter:
echo readTwitterShares(get_permalink());
Add Twitter and Facebook info fields
By default, WordPress allows you to add various contact info as such as Jabber, AIM and Yahoo Messenger. But there’s no way to add your Twitter and Facebook info. Here’s a handy code snippet to remove the mostly useless YIM, AIM and Jabber and add Twitter and Facebook info instead.
This code goes to your functions.php file.
function new_contactmethods( $contactmethods ) { $contactmethods['twitter'] = 'Twitter'; // Add Twitter $contactmethods['facebook'] = 'Facebook'; // Add Facebook unset($contactmethods['yim']); // Remove YIM unset($contactmethods['aim']); // Remove AIM unset($contactmethods['jabber'<wbr>]); // Remove Jabber return $contactmethods; } add_filter('user_<wbr>contactmethods','new_<wbr>contactmethods',10,1); </wbr></wbr></wbr>