Add Tweets to Wordpress

A month ago, Roxanne got her new Web site, which incorporated her latest Tweet in her footer. It’s a nifty idea I’ve seen on several other sites, and I knew Roxanne would like it. However, it was not near as easy to implement as it should have been.

Due to some unknown server configuration, Wordpress had trouble grabbing the Twitter RSS feed with any consistency. No matter which plugin I used, error messages occurred 90% of the time. I spent at least 10 hours researching the subject and participating in a small discussion on the matter.

I’ve finally devised a workaround by using Twitter’s Atom feed instead of RSS. For the last four days it has worked without a hitch. All the function does is grab the most recent tweet and prints it along with how long ago it was tweeted.

Follow the link below for the code, and feel free to see it working on Roxanne’s page.

Twitterpate function.

To use, insert the following into your theme or template:

1
<?php echo Twitterpate(); ?>

You need a functions.php file in your theme directory. If it does not exist for your theme, create it in your theme directory and wrap <?php before the function and ?> after it.)

Feel free to customize the HTML on lines 6, 8, & 26 to suit your needs.

Insert the following into functions.php, replacing the username:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function Twitterpate() {
 
	// Your twitter username.
	$username = "JoeSchmoe";
 
	$prefix = "<span>Latest Tweet:</span> ";
 
	$suffix = "";
 
	$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
 
	function parse_feed($feed) {
 
		/*** Getting Tweet text ***/
		$step_one = explode("<content type=\"html\">", $feed);
		$step_two = explode("</content>", $step_one[1]);
		$tweet = $step_two[0];
		$tweet = str_replace("&lt;", "<", $tweet);
		$tweet = str_replace("&gt;", ">", $tweet);
 
		/*** Getting date in human time difference ***/
		preg_match('~<published>(.*?)</published>~', $feed, $match);
		if (!empty($match)) $date_string = iso8601_to_timestamp($match[0]);
			else $date_string = '';
		if ($date_string != '') {
			$date_string = '<abbr>' . human_time_diff($date_string) . ' ago</abbr>';
			$tweet .= $date_string;
		}
 
		return $tweet;
	}
 
	function iso8601_to_timestamp($datestr) {
		$eregStr =
		'([0-9]{4})-'.  // centuries & years CCYY-
		'([0-9]{2})-'.  // months MM-
		'([0-9]{2})'.   // days DD
		'T'.                    // separator T
		'([0-9]{2}):'.  // hours hh:
		'([0-9]{2}):'.  // minutes mm:
		'([0-9]{2})(\.[0-9]+)?'. // seconds ss.ss...
		'(Z|[+\-][0-9]{2}:?[0-9]{2})?'; // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's
		if(ereg($eregStr,$datestr,$regs)){
			// not utc
			if($regs[8] != 'Z'){
				$op = substr($regs[8],0,1);
				$h = substr($regs[8],1,2);
				$m = substr($regs[8],strlen($regs[8])-2,2);
				if($op == '-') {
					$regs[4] = $regs[4] + $h;
					$regs[5] = $regs[5] + $m;
				}
				elseif($op == '+') {
					$regs[4] = $regs[4] - $h;
					$regs[5] = $regs[5] - $m;
				}
			}
			return strtotime("$regs[1]-$regs[2]-$regs[3] $regs[4]:$regs[5]:$regs[6]Z");
		}
		else return false;
	}
 
 
	$twitterFeed = file_get_contents($feed);
	return stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
 
}

Credit goes to: Mark Essel for the foundation of the function and XooFoo for the iso8601_to_timestamp function.


Leave a Reply

You must be logged in to post a comment.