How to show your latest tweets on your WordPress site without a plugin

Showing your latest tweets on your WordPress site is a good way to help your readers staying tuned with you, and doing it became a such common task. A plug-in can do that job but it’s simple tasks and if you are WordPress themer you probably should do it without a plug-in.

If you want to show only the latest tweet on your site, the method Ryan Barr suggested is good to go. (Although he said this method can show multiple tweets with changing the number of rpp on line 12 but it’s never worked for me. Also don’t forget add line 21.)

Method for displaying the latest tweet

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
<?php
// Your twitter username.
$username = "TwitterUsername";
 
// Prefix - some text you want displayed before your latest tweet. 
// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
$prefix = "";
 
// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
$suffix = "";
 
$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);
   $tweet = str_replace("&quot;", "", $tweet);
   return $tweet;
}
 
$twitterFeed = file_get_contents($feed);
 
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>


UPDATE
However, if you want to show more than one tweets, the method InstantShift presented is the best way! With this method we are even able to show tweets from multiple accounts! I modified their code to make links clickable and quote & apostrophe appearing as they should be.

Method for displaying more than one tweets from account(s)

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
<?php
$usernames = "YourTwitterUsername"; // Pull from accounts, separated by a space
$limit = "3"; // Number of tweets to pull in, total.
$show = 0; // Show username? 0 = No, 1 = Yes.
 
$prefix = "<ul>"; // This comes before the entire block of tweets.
$prefix_sub = "<li>"; // This comes before each tweet on the feed.
$wedge = "<br />"; // This comes after the username but before the tweet content.
$suffix_sub = "</li>"; // This comes after each tweet on the feed.
$suffix = "</ul>"; // 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]);
 
    	// Make the links clickable and take care quote & apostrophe
 
    	$clean_content[0] = str_replace("&lt;", "<", $clean_content[0]); 
    	$clean_content[0] = str_replace("&gt;", ">", $clean_content[0]); 
    	$clean_content[0] = str_replace("&amp;", "&", $clean_content[0]); 
    	$clean_content[0] = str_replace("&quot;", "\"", $clean_content[0]);
    	$clean_content[0] = str_replace("&apos;", "'", $clean_content[0]);
 
    	echo $prefix_sub;
 
    	if ($show == 1) { 
    		echo  "<a href=\"" . $clean_uri[0] . "\" class=\"twitterlink\">" . $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;
?>

11 thoughts on “How to show your latest tweets on your WordPress site without a plugin

  1. Pingback: Resources About Wordpress Hacks And Customizations

  2. Thanks for the code from Instant Shift but I have a question. The output doesn’t know how to handle the ” in my tweets so for example a Tweet with I’m shows up on my website as I'm. Any idea how to fix this?

  3. Hi Harold,
    Thank you for pointing out. I found a better way which fixes quote and apostrophe so I update this post. With this method, we are even able to show tweets from multiple accounts!!

  4. Pingback: Wordpress Hacks And Customizations « MoeMir

  5. Pingback: Wordpress Hacks e Customizations! | sinergie.::.websolution | il blog

  6. Pingback: sinergie.::.websolution » Blog Archive » Wordpress Hacks e Customizations!

  7. Thank you very much this code !

    But i’m tottally beginner in php and i would like to add the attribute tagert=”_blank” to the links. I think it’s at line 45 but i don’t know the right syntax.

    ps : sorry for my english i’m french :p

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">