WordPress snippets to interact with social networks

by Jean. 5 Comments -

Social networks are a very important way to publicize your blog and attract new visitors. So you definitely need to make sure that your website or blog interacts with Facebook, Twitter, Google + and other networks. In this article, I have compiled a list of super useful snippets to interact with popular social networks.

Display number of Facebook fans in full text on your WordPress blog

If you have a Facebook page for your blog, you might want to display how many fans you have. Simply paste the following code in any of your theme files, where you want your Facebook fan count to be displayed. Don’t forget to add your page ID on line 2!

<?php
	$page_id = "YOUR PAGE-ID";
	$xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot");
	$fans = $xml->page->fan_count;
	echo $fans;
?>

Source: http://wp-snippets.com/display-number-facebook-fans/

Automatically add Twitter and Facebook buttons to your posts

Here is a great snippet to provide Twitter and Facebook buttons to let your visitors share your content with their friends/followers.

Just add the code in your functions.php and save the file.

function share_this($content){
    if(!is_feed() && !is_home()) {
        $content .= '<div class="share-this">
                    <a href="http://twitter.com/share"
class="twitter-share-button"
data-count="horizontal">Tweet</a>
                    <script type="text/javascript"
src="http://platform.twitter.com/widgets.js"></script>
                    <div class="facebook-share-button">
                        <iframe
src="http://www.facebook.com/plugins/like.php?href='.
urlencode(get_permalink($post->ID))
.'&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=like&amp;colorscheme=light&amp;height=21"
scrolling="no" frameborder="0" style="border:none;
overflow:hidden; width:200px; height:21px;"
allowTransparency="true"></iframe>
                    </div>
                </div>';
    }
    return $content;
}
add_action('the_content', 'share_this');

Source: http://www.wprecipes.com/automatically-add-twitter-and-facebook-buttons-to-your-posts

Automatically link Twitter usernames in WordPress

If you’re often quoting Twitter users on your blog, what about automatically link twitter usernames? Here is a nice regular expression to do so. Simply add this code to your functions.php file for it to work.

function twtreplace($content) {
	$twtreplace = preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/',"$1<a href=\"http://twitter.com/$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>",$content);
	return $twtreplace;
}

add_filter('the_content', 'twtreplace');   
add_filter('comment_text', 'twtreplace');

Source: http://snipplr.com/view/70977/automatically-link-twitter-usernames-in-wordpress/

Show a total share counter (FB, Twitter, G+)

Want to display how many times your post has been shared on Facebook, Twitter and Google+? This is what the snippet below will do for you. Paste it where you want the count to be displayed, within the loop.

function social_shares() {
    $url = get_permalink( $post_id ); 
    $json = file_get_contents(&quot;http://api.sharedcount.com/?url=" .
rawurlencode($url));
    $counts = json_decode($json, true);
    $totalcounts= $counts[&quot;Twitter&quot;] + 
$counts[&quot;Facebook&quot;][&quot;total_count&quot;] +
$counts[&quot;GooglePlusOne&quot;];
    echo &quot;&lt;div&gt;$totalcounts Share&lt;/div&gt;&quot;;
}

Source: http://www.wprecipes.com/wordpress-function-to-show-a-total-share-counter-fb-twitter-g

Automatically add a Google+ button to your posts

I shown you how to add Facebook and Twitter buttons to your posts, but what about Google +? Here’s a snippet that will add a G+ button so your visitors can share them with their circles. Just past the code below in your functions.php and you’re done.

add_filter('the_content', 'wpr_google_plusone');
function wpr_google_plusone($content) {
	$content = $content.'<div class="plusone"><g:plusone size="tall" href="'.get_permalink().'"></g:plusone></div>';
	return $content;
}
add_action ('wp_enqueue_scripts','wpr_google_plusone_script');
function wpr_google_plusone_script() {
	wp_enqueue_script('google-plusone', 'https://apis.google.com/js/plusone.js', array(), null);
}

Source: http://spyrestudios.com/17-time-saving-code-snippets-for-wordpress-developers/

Display your latest Google+ update on your WordPress blog

Are you using Google +, Google social network? If yes, you can easily display your latest G+ update on your WordPress blog using this code snippet.

Copy the code below, and paste it where you want to display your latest Google update. That’s it!

<?php
	include_once(ABSPATH.WPINC.'/rss.php');
	$googleplus = fetch_feed("http://plusfeed.appspot.com/103329092193061943712"); // Replace 103329092193061943712 by your own ID
	echo '<a href="';
	echo $googleplus->items[0]['link']; echo '">';
	echo $googleplus->items[0]['summary'];
	echo '';
?>

Source: http://www.geekeries.fr/snippet/afficher-mise-a-jour-google-plus/

Add a Pinterest “pin it” button for your WordPress blog

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/pin/create/button/?url=<?php the_permalink(); ?>&media=<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' ); echo $thumb['0']; ?>&description=<?php the_title(); ?>" class="pin-it-button" count-layout="horizontal">Pin It</a>

Once done, open your footer.php file and add the pinterest JavaScript code:

<script type="text/javascript" src="http://assets.pinterest.com/js/pinit.js"></script>

Source: http://www.marketingtechblog.com/wordpress-pinterest-button/

  • http://www.ljasinski.pl/blog ljasinskipl

    Seriously? Did you check that or are you only spinning old content? Twitter rest api was closed 6 months ago and your snippet is still using it.

    • http://wpti.ps Piet

      This site is lately only republishing old content and the author doesn’t seem to be too bothered with checking whether or not the (his?) code is still valid.

      The code to “Display your latest Google+ update on your WordPress blog” is also not working as that feed site is throwing errors.

      A shame really as this site used to be quite good and handy…

      Time to unsubscribe from the feed methinks…

  • Jamie

    Smart snippets. These should be done to improve social media engagement.

  • http://www.bestskincarezone.com Jamie

    These are cool! Showcasing my other social media information and stats on my blog.

  • http://www.bestskincarezone.com Jamie

    These are nice! I can fully integrate my other social networking accounts and activities in my blog.