10 awesome Twitter Hacks For Your WordPress Blog

01. Create a “Twitter This” Button for WordPress Posts

InstantShift - Twitter Hacks For Your WordPress Blog

Creating a “Tweet This” button for your blog posts are fairly very easy. Use this following code on your single.php file, within the loop to create a button. This is the basic code for a typical “Twitter This” Button, you can use some images or CSS styling to make it more attractive. Possibilities are endless.

1 <a href=" reading <?php the_permalink(); ?>" title="Send this page to Twitter!" target="_blank">Spread on Twitter</a>

02. Automatically Create TinyUrls for Your Tweets

InstantShift - Twitter Hacks For Your WordPress Blog

You have to use a URL shortening services when tweeting URLs because of 140 characters restriction in Twitter. For that purpose you can use tinyurls, because tinyurl provides an API, which takes over this task easily and generates unique tinyurls for your blog posts automatically.

To use tinyurl API, edit your functions.php file and paste the following function code:

1 function getTinyUrl($url) {
2   $tinyurl file_get_contents("".$url);
3   return $tinyurl;
4 }

Now in your single.php file, paste the following within the loop:

1 <?php
2   $turl = getTinyUrl(get_permalink($post->ID));
3   echo 'Tiny Url for this post: <a href="'.$turl.'">'.$turl.'</a>'
4 ?>

03. Displaying Total Number Twitter Followers on WordPress

InstantShift - Twitter Hacks For Your WordPress Blog

You are actively using Twitter from last few weeks and the numbers of followers are increasing regularly. So you are looking for way to display total number of your Twitter followers on your blog and update automatically. You can use this piece of code to display your followers anywhere you want, may be you want to display this number in author info in your blog posts.

Open functions.php and paste this php function there.

1 function string_getInsertedString($long_string,$short_string,$is_html=false){
2   if($short_string>=strlen($long_string))return false;
3   $insertion_length=strlen($long_string)-strlen($short_string);
4   for($i=0;$i<strlen ($short_string);++$i){
5     if($long_string[$i]!=$short_string[$i])break;
6   }
7   $inserted_string=substr($long_string,$i,$insertion_length);
8   if($is_html && $inserted_string[$insertion_length-1]=='<'){
9     $inserted_string='<'.substr($inserted_string,0,$insertion_length-1);
10   }
11   return $inserted_string;
12 }
14 function DOMElement_getOuterHTML($document,$element){
15   $html=$document->saveHTML();
16   $element->parentNode->removeChild($element);
17   $html2=$document->saveHTML();
18   return string_getInsertedString($html,$html2,true);
19 }
21 function getFollowers($username){
22   $x file_get_contents("".$username);
23   $doc new DomDocument;
24   @$doc->loadHTML($x);
25   $ele $doc->getElementById('follower_count');
26   $innerHTML=preg_replace('/^< [^>]*>(.*)< [^>]*>$/',"\\1",DOMElement_getOuterHTML($doc,$ele));
27   return $innerHTML;
28 }

Now simply use this code to display total number of your Twitter followers. Just replace our username (instantshift) with yours.

1 <?php echo getFollowers("instantshift")." followers"; ?>

Of course you can further style it with CSS. You can also try some fancy background images or twitter bird. For example.

1 <span style="background:#C00;border:#FFF 1px solid;color:#FFF;font-family:'Myriad Pro',Helvetica,Arial,sans-serif;font-size:28px;padding:10px 20px;font-weight:bold;width:auto;">Proudly Followed by <?php echo getFollowers("google"); ?> Followers</span>

04. Use Twitter Avatars in Comments

InstantShift - Twitter Hacks For Your WordPress Blog

Thousands of blogs show avatars next to their user’s comments. The avatars are a great way to make things more personal and create some variety between the different comments. You can display Twitter avatars in your WordPress comments, instead of gravatars. Smashing Magazine provided fairly easy way to integrates Twitter avatars on your comments.

The first thing you need to do is to get the functions file here. Once you downloaded the file, unzip the archive and open the twittar.php file. Select all its content and paste it into functions.php. Finally you will need to enable Twitter avatars in your comments.php file. Edit comments.php and past this code in comments loop. Now you are ready with Twitter avatars.

1 <?php twittar('45''default.png''#e9e9e9''twitavatars', 1, 'G'); ?>

05. Display Your Most Recent Twitter Entry

InstantShift - Twitter Hacks For Your WordPress Blog

Here is a small script for WordPress blog that allows us to pull the latest tweet from a Twitter user via the RSS feed that is produced by Twitter. We are also able to set a prefix and a suffix for the tweet for further customization.

1 <?php
2 $username "TwitterUsername"// Your twitter username.
3 $prefix ""// Prefix - some text you want displayed before your latest tweet.
4 $suffix ""// Suffix - some text you want display after your latest tweet.
5 $feed "" $username "&rpp=1";
7 function parse_feed($feed) {
8 $stepOne explode("<content type=\"html\">"$feed);
9 $stepTwo explode("</content>"$stepOne[1]);
10 $tweet $stepTwo[0];
11 $tweet str_replace("&lt;""<"$tweet);
12 $tweet str_replace("&gt;"">"$tweet);
13 return $tweet;
14 }
16 $twitterFeed file_get_contents($feed);
17 echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
18 ?>

06. Display X Number of Your Latest Twitter Entries

InstantShift - Twitter Hacks For Your WordPress Blog

Now we are going to introduce the ability to pull numerous tweets. With limits set in the Twitter API, we can pull anywhere from 1 to 100. Though it is nice to be able to pull so many, we have to remember that the more we pull, the longer it will take for this script to generate its content.

1 <?php
2 $username "TwitterUsername"// Your twitter username.
3 $limit "5"// Number of tweets to pull in.
5 /* These prefixes and suffixes will display before and after the entire block of tweets. */
6 $prefix ""// Prefix - some text you want displayed before all your tweets.
7 $suffix ""// Suffix - some text you want displayed after all your tweets.
8 $tweetprefix ""// Tweet Prefix - some text you want displayed before each tweet.
9 $tweetsuffix "<br>"// Tweet Suffix - some text you want displayed after each tweet.
11 $feed "" $username "&rpp=" $limit;
13 function parse_feed($feed$prefix$tweetprefix$tweetsuffix$suffix) {
15 $feed str_replace("&lt;""<"$feed);
16 $feed str_replace("&gt;"">"$feed);
17 $clean explode("<content type=\"html\">"$feed);
19 $amount count($clean) - 1;
21 echo $prefix;
23 for ($i = 1; $i <= $amount$i++) {
24 $cleaner explode("</content>"$clean[$i]);
25 echo $tweetprefix;
26 echo $cleaner[0];
27 echo $tweetsuffix;
28 }
30 echo $suffix;
31 }
33 $twitterFeed file_get_contents($feed);
34 parse_feed($twitterFeed$prefix$tweetprefix$tweetsuffix$suffix);
35 ?>

07. Display Tweets From Multiple Users

InstantShift - Twitter Hacks For Your WordPress Blog

Now what you are looking for? Maybe you want to pull multiple tweets from multiple user accounts and display them. With the code below, you are able to define what you want before the Twitter block, after the Twitter block, before and after each individual tweet, and after the displayed user name. You can also turn off the displayed user names if you are looking to show all of your own personal tweets from multiple accounts!

1 <?php
2 $usernames "Username Username Username"// Pull from accounts, separated by a space
3 $limit "5"// Number of tweets to pull in, total.
4 $show = 1; // Show username? 0 = No, 1 = Yes.
6 $prefix ""// This comes before the entire block of tweets.
7 $prefix_sub ""// This comes before each tweet on the feed.
8 $wedge ""// This comes after the username but before the tweet content.
9 $suffix_sub "<br>"// This comes after each tweet on the feed.
10 $suffix ""// This comes after the entire block of tweets.
12 function parse_feed($usernames$limit$show$prefix_sub$wedge$suffix_sub) {
14 $usernames str_replace(" ""+OR+from%3A"$usernames);
15 $feed "" $usernames "&rpp=" $limit;
16 $feed file_get_contents($feed);
17 $feed str_replace("&""&"$feed);
18 $feed str_replace("<""<"$feed);
19 $feed str_replace(">"">"$feed);
20 $clean explode("<entry>"$feed);
21 $amount count($clean) - 1;
23 for ($i = 1; $i <= $amount$i++) {
25 $entry_close explode("</entry>"$clean[$i]);
26 $clean_content_1 explode("<content type=\"html\">"$entry_close[0]);
27 $clean_content explode("</content>"$clean_content_1[1]);
28 $clean_name_2 explode("<name>"$entry_close[0]);
29 $clean_name_1 explode("("$clean_name_2[1]);
30 $clean_name explode(")</name>"$clean_name_1[1]);
31 $clean_uri_1 explode("<uri>"$entry_close[0]);
32 $clean_uri explode("</uri>"$clean_uri_1[1]);
34 echo $prefix_sub;
35 if ($show == 1) { echo "<a href=\"" $clean_uri[0] . "\">" $clean_name[0] . "</a>" .$wedge; }
36 echo $clean_content[0];
37 echo $suffix_sub;
39 }
40 }
41 echo $prefix;
42 parse_feed($usernames$limit$show$prefix_sub$wedge$suffix_sub);
43 echo $suffix;
44 ?>

08. Display Latest Tweet as an Image

InstantShift - Twitter Hacks For Your WordPress Blog

TwitSig is a twitter signature service provider that allows you to get an auto-updating image that displays your latest Twitter entry. You can use this image as your forum signatures, in WordPress posts, in blog sidebars or may be even in emails.

Go to and enter your Twitter username. And you are good to go with your Twitsig image displaying your latest tweet. The image is automatically updated when you update your Twitter status. Now you can show your latest tweet using following code wherever you want.

1 <a href=""><img src=""/></a>

09. Detect Visitors From Twitter

InstantShift - Twitter Hacks For Your WordPress Blog

If you have significant visitor base from Twitter then will it not be a great idea to detect Twitter visitors and offer them a warm welcome, remind them that their re-tweets are appreciated. You might also want them to point towards some specific blog posts or some freebies that they might interested in.

For that you can add following code in your single.php file and add your welcome message.

1 <?php if (strpos("",$_SERVER[HTTP_REFERER])==0) {
2 echo "Welcome, Twitter visitor! If you enjoy this post, don't hesitate to retweet!";
3 } ?>

10. Your Twitter Feed on a Separate WordPress Page

InstantShift - Twitter Hacks For Your WordPress Blog

You might want to create a separate page on your WordPress blog for displaying your latest tweets and updates. For that Smashing Magazine suggested a solution using “Page template” feature in WordPress. First you will need to create a Twitter page template “twitter-page.php” (you can use different name) to display Twitter feed, paste the following code and add it to your blog. Simple enough, huh!

1 <?php
3 /*
4 Template Name: Twitter page
5 */
7 get_header();
9 include_once(ABSPATH.WPINC.'/rss.php');
10 wp_rss('', 20);
12 get_sidebar();
13 get_footer();
14 ?>

Find Something Missing?

Feel free to share any hack that you think would be a great addition in this collection and that has not been listed already.


Posted on February 21, 2012, in wordpress and tagged , , . Bookmark the permalink. 6 Comments.

  1. I have recieved inspiration from your posts.I have started visiting your website everyday to get something for new.Thank you

  2. What¡¦s Happening i am new to this, I stumbled upon this I have discovered It positively useful and it has aided me out loads. I’m hoping to give a contribution & help other customers like its helped me. Good job.

  3. It is really a great and helpful piece of info. I¡¦m happy that you simply shared this useful information with us. Please keep us up to date like this. Thank you for sharing.

  4. Undeniably believe that which you stated. Your favorite justification appeared to be on the web the easiest thing to be aware of. I say to you, I definitely get irked while people consider worries that they just do not know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people could take a signal. Will probably be back to get more. Thanks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: