15 Useful WordPress Codes & Plugins Related to Twitter


WordPress development is quite on hike, as it is the most popular blogging CMS around. Lots of developers out there try to implement codes related to WordPress to increase the usability of the Platform and try to discover new possibilities. Previously we have published few useful articles on WordPress hacks and WordPress codes to help you out defining WordPress security and many other things. Today, again we are here to bring some taste of twitter to your WordPress site. Following 15 useful WordPress code snippets will help you out in integrating Twitter and to do other Twitter related stuff on your WP Blogs.


twitter integration in WordPress



1. Display the Website’s Total Number of Twitter Followers

[sourcecode language=”php”]
function string_getInsertedString($long_string,$short_string,$is_html=false){
if($short_string>=strlen($long_string))return false;
for($i=0;$i<strlen ($short_string);++$i){
if($is_html && $inserted_string[$insertion_length-1]=='<‘){
return $inserted_string;

function DOMElement_getOuterHTML($document,$element){
return string_getInsertedString($html,$html2,true);

function getTwitterFollowers($username){
$x = file_get_contents("http://twitter.com/".$username);
$doc = new DomDocument;
$ele = $doc->getElementById(‘follower_count’);
$innerHTML=preg_replace(‘/^< [^>]*>(.*)< [^>]*>$/’,"\\1",DOMElement_getOuterHTML($doc,$ele));
return $innerHTML;


2. Automatically Create Short URLs for Posts in Tweets

[sourcecode language=”php”]
function makeBitly($url) {
$tinyurl = file_get_contents("http://bit.ly/api-create.php?url=".$url);
return $bitly;
That line of PHP code must be placed within the WordPress Loop, and it looks like the following:
[sourcecode language=”php”]
<?php $burl = makeBitly(get_permalink($post->ID)); echo ‘The short URL for this post: < a href="’.$burl.’" >’.$burl.'< /a >’ ? >


3. Tweet from the WordPress Dashboard

Known as Twit-Twoo, the plug-in allows Tweeting from within the Dashboard by using a modular box on the Dashboard’s homepage. Tweets are instantly published to the service once the administrator’s Twitter account is linked to it properly.

Tweet from the WordPress Dashboard


4. Create a Page Specifically Used to Display Tweets

[sourcecode language=”php”]
Template Name: A Page for Tweets


wp_rss(‘http://twitter.com/statuses/user_timeline/RSSfilename.rss’, 35);




5. Display the Latest Tweet on the WordPress Site

[sourcecode language=”php”]
$username = "YourTwitterUsername"; // Put your username here.
$prefix = "My Latest Tweet"; // Any text that goes before the tweet itself.
$suffix = ""; // Any text that should be placed directly after the 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("&lt;", "<", $tweet);
$tweet = str_replace("&gt;", ">", $tweet);
return $tweet;

$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);


6. Post a Regular Blog Entry Containing a Digest of Recent Tweets

The best way to do this within WordPress is to use the Twitter Tools plug-in. The plug-in is one of the best ways to deeply integrate with Twitter, and it has its own feature for tweet digests. After installation and activation, browse to the Dashboard’s sidebar and find “Twitter Tools” in the Settings area.


7. Enable “Tweetbacks” as an Alternative to Traditional Pingbacks

To enable this feature, administrators will need to install the Tweetbacks plug-in from WordPress.org, or from within their Dashboard. A few configuration settings will need to be defined, and then the plug-in will be ready to go with a simple template modification in the comments.php file.

Enable Tweetbacks as an Alternative to Traditional Pingbacks


8. Display the Latest Tweets from Multiple Authors

[sourcecode language=”php”]
$usernames = "Profile1 Profile2 Profile3 Profile4"; // Usernames go here, separated by a space.
$limit = "5"; // Define the maximum number of tweets to show
$show = 1; // Change to 0 if account usernames should not be shown, leave set to 1 if they should be
$prefix = "Our Latest Tweets"; // A prefix or heading for the tweets
$prefix_sub = ""; //Prefix for each individual tweet
$wedge = ""; // Separator between tweets
$suffix_sub = "<br>"; // Appended to the end of every tweet
$suffix = ""; // This section of code will be added at the end of the entire list 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;


9. Automatically Create a New Tweet When a New Post is Published

Using the Twitter Tools plug-in, administrators can configure a setting so that a new tweet is posted whenever new post is published in the WordPress Dashboard.

Automatically Create a New Tweet When a New Post is Published


10. Add a News-Style Ticker of Tweets to the Site

One of the best ways to conserve space and keep readers up to date about activities on Twitter is to add a ticker to the site that rotates the latest tweets like a typical news ticker does. Best of all, this is easy to accomplish with a basic WordPress plug-in known as My Twitter Ticker.

Add a News-Style Ticker of Tweets to the Site


11. Display the Latest Tweet as an Image

[sourcecode language=”php”]
<a href="http://twitter.com/YourProfileUsername">
<img src="http://twitsig.com/YourProfileUsername.jpg" alt="Image of Tweets" title "Image of Tweets" class="tweetImage" />


12. Create a “Tweet This” Button to Be Displayed with Posts

[sourcecode language=”html”]
<a href="http://twitter.com/home?status=I think you should read <?php the_permalink(); ?>" title="Send to Twitter">Tweet About This Post!</a>


13. Show a Retweet Button with a Counter with Each Post

[sourcecode language=”javascript”]
<script type="text/javascript">
tweetmeme_source = ‘YourTwitterUsername’;
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script>


14. Use Twitter Avatars in Comments

Another great way to encourage integration between Twitter and WordPress is to use visitors’ Twitter profile pictures as avatars in comments on the site. That’s done by using a plug-in called Twittar.
[sourcecode language=”php”]
<?php twittar(’30’, ‘default-avatar.jpg’, ‘X’); ?>

Use Twitter Avatars in Comments


15. Detect Visitors From Twitter

[sourcecode language=”php”]
<?php if (strpos("twitter.com",$_SERVER[HTTP_REFERER])==0) { echo "Hey, Twitter enthusiast! We’re glad you’re here. If you like what you see, be sure to Tweet about it and follow our account!"; } ?>