10 Twitter Hacks For Your WordPress Blog
01. Create a “Twitter This” Button for WordPress Posts

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.
- <a href=”http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>” title=”Send this page to Twitter!” target=”_blank”>Spread on Twitter</a>
02. Automatically Create TinyUrls for Your Tweets

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:
- function getTinyUrl($url) {
- $tinyurl = file_get_contents(“http://tinyurl.com/api-create.php?url=”.$url);
- return $tinyurl;
- }
Now in your single.php file, paste the following within the loop:
- <?php
- $turl = getTinyUrl(get_permalink($post->ID));
- echo ’Tiny Url for this post: <a href=”‘.$turl.’”>’.$turl.’</a>’
- ?>
03. Displaying Total Number Twitter Followers on WordPress

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.
- function string_getInsertedString($long_string,$short_string,$is_html=false){
- if($short_string>=strlen($long_string))return false;
- $insertion_length=strlen($long_string)-strlen($short_string);
- for($i=0;$i<strlen ($short_string);++$i){
- if($long_string[$i]!=$short_string[$i])break;
- }
- $inserted_string=substr($long_string,$i,$insertion_length);
- if($is_html && $inserted_string[$insertion_length-1]==’<’){
- $inserted_string=’<’.substr($inserted_string,0,$insertion_length-1);
- }
- return $inserted_string;
- }
- function DOMElement_getOuterHTML($document,$element){
- $html=$document->saveHTML();
- $element->parentNode->removeChild($element);
- $html2=$document->saveHTML();
- return string_getInsertedString($html,$html2,true);
- }
- function getFollowers($username){
- $x = file_get_contents(“http://twitter.com/”.$username);
- $doc = new DomDocument;
- @$doc->loadHTML($x);
- $ele = $doc->getElementById(‘follower_count’);
- $innerHTML=preg_replace(‘/^< [^>]*>(.*)< [^>]*>$/’,”\\1″,DOMElement_getOuterHTML($doc,$ele));
- return $innerHTML;
- }
Now simply use this code to display total number of your Twitter followers. Just replace our username (instantshift) with yours.
- <?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.
- <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

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.
- <?php twittar(’45′, ’default.png’, ’#e9e9e9′, ’twitavatars’, 1, ’G'); ?>
05. Display Your Most Recent Twitter Entry

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.
- <?php
- $username = ”TwitterUsername”; // Your twitter username.
- $prefix = ”"; // Prefix - some text you want displayed before your latest tweet.
- $suffix = ”"; // Suffix - some text you want display after your latest tweet.
- $feed = ”http://search.twitter.com/search.atom?q=from:” . $username . ”&rpp=1″;
- function parse_feed($feed) {
- $stepOne = explode(“<content type=\”html\”>”, $feed);
- $stepTwo = explode(“</content>”, $stepOne[1]);
- $tweet = $stepTwo[0];
- $tweet = str_replace(“<”, ”<”, $tweet);
- $tweet = str_replace(“>”, ”>”, $tweet);
- return $tweet;
- }
- $twitterFeed = file_get_contents($feed);
- echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
- ?>
06. Display X Number of Your Latest Twitter Entries

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.
- <?php
- $username = ”TwitterUsername”; // Your twitter username.
- $limit = ”5″; // Number of tweets to pull in.
- /* These prefixes and suffixes will display before and after the entire block of tweets. */
- $prefix = ”"; // Prefix - some text you want displayed before all your tweets.
- $suffix = ”"; // Suffix - some text you want displayed after all your tweets.
- $tweetprefix = ”"; // Tweet Prefix - some text you want displayed before each tweet.
- $tweetsuffix = ”<br>”; // Tweet Suffix - some text you want displayed after each tweet.
- $feed = ”http://search.twitter.com/search.atom?q=from:” . $username . ”&rpp=” . $limit;
- function parse_feed($feed, $prefix, $tweetprefix, $tweetsuffix, $suffix) {
- $feed = str_replace(“<”, ”<”, $feed);
- $feed = str_replace(“>”, ”>”, $feed);
- $clean = explode(“<content type=\”html\”>”, $feed);
- $amount = count($clean) - 1;
- echo $prefix;
- for ($i = 1; $i <= $amount; $i++) {
- $cleaner = explode(“</content>”, $clean[$i]);
- echo $tweetprefix;
- echo $cleaner[0];
- echo $tweetsuffix;
- }
- echo $suffix;
- }
- $twitterFeed = file_get_contents($feed);
- parse_feed($twitterFeed, $prefix, $tweetprefix, $tweetsuffix, $suffix);
- ?>
07. Display Tweets From Multiple Users

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!
- <?php
- $usernames = ”Username Username Username”; // Pull from accounts, separated by a space
- $limit = ”5″; // Number of tweets to pull in, total.
- $show = 1; // Show username? 0 = No, 1 = Yes.
- $prefix = ”"; // This comes before the entire block of tweets.
- $prefix_sub = ”"; // This comes before each tweet on the feed.
- $wedge = ”"; // This comes after the username but before the tweet content.
- $suffix_sub = ”<br>”; // This comes after each tweet on the feed.
- $suffix = ”"; // This comes after the entire block of tweets.
- function parse_feed($usernames, $limit, $show, $prefix_sub, $wedge, $suffix_sub) {
- $usernames = str_replace(“ ”, ”+OR+from%3A”, $usernames);
- $feed = ”http://search.twitter.com/search.atom?q=from%3A” . $usernames . ”&rpp=” . $limit;
- $feed = file_get_contents($feed);
- $feed = str_replace(“&”, ”&”, $feed);
- $feed = str_replace(“<”, ”<”, $feed);
- $feed = str_replace(“>”, ”>”, $feed);
- $clean = explode(“<entry>”, $feed);
- $amount = count($clean) - 1;
- for ($i = 1; $i <= $amount; $i++) {
- $entry_close = explode(“</entry>”, $clean[$i]);
- $clean_content_1 = explode(“<content type=\”html\”>”, $entry_close[0]);
- $clean_content = explode(“</content>”, $clean_content_1[1]);
- $clean_name_2 = explode(“<name>”, $entry_close[0]);
- $clean_name_1 = explode(“(“, $clean_name_2[1]);
- $clean_name = explode(“)</name>”, $clean_name_1[1]);
- $clean_uri_1 = explode(“<uri>”, $entry_close[0]);
- $clean_uri = explode(“</uri>”, $clean_uri_1[1]);
- echo $prefix_sub;
- if ($show == 1) { echo ”<a href=\”" . $clean_uri[0] . ”\”>” . $clean_name[0] . ”</a>” . $wedge; }
- echo $clean_content[0];
- echo $suffix_sub;
- }
- }
- echo $prefix;
- parse_feed($usernames, $limit, $show, $prefix_sub, $wedge, $suffix_sub);
- echo $suffix;
- ?>
08. Display Latest Tweet as an Image

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 Twitsig.com 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.
- <a href=”http://twitter.com/instantshift”><img src=”http://twitsig.com/instantshift.jpg”/></a>
09. Detect Visitors From Twitter

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.
- <?php if (strpos(“twitter.com”,$_SERVER[HTTP_REFERER])==0) {
- echo ”Welcome, Twitter visitor! If you enjoy this post, don’t hesitate to retweet!”;
- } ?>
10. Your Twitter Feed on a Separate WordPress Page

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!
- <?php
- /*
- Template Name: Twitter page
- */
- get_header();
- include_once(ABSPATH.WPINC.’/rss.php’);
- wp_rss(‘http://twitter.com/statuses/user_timeline/16906892.rss’, 20);
- get_sidebar();
- get_footer();
- ?>
