PeachPay

Sponsored

WP Mayor is supported by its audience. Our recommendations are based on our experts’ research and hands-on testing. If you purchase through a link on our website, we may earn a commission. Here’s why you can trust us.

How to Display Your Twitter Status on Your WordPress Site

  Introduction
Let's take a look at some of the ways you can display your latest tweets on your site without using plugins.
WP Mayor is supported by its audience. Our recommendations are based on our experts’ research and hands-on testing. If you purchase through a link on our website, we may earn a commission. Here’s why you can trust us.
 Table of contents

Let’s take a look at some of the ways you can display your latest tweets on your site without using plugins.

Using cURL

Replace ‘wpmayor’ with your Twitter username in the get_status() function call. You can put the function in your functions.php file, and call the function wherever you like within your theme.

[php]

function get_status($twitter_id, $hyperlinks = true) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=1");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$src = curl_exec($c);
curl_close($c);
preg_match(‘/(.*)/’, $src, $m);
$status = htmlentities($m[1]);
if( $hyperlinks ) $status = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href="\&quot;\\0\&quot;">\\0</a>", $status);
return($status);
}
echo get_status(‘wpmayor’);

[/php]

Source: CatsWhoCode

Using RSS and Caching

This code uses caching, and lets you select the number of tweets returned. Just replace your username in the function call, and enter the number of tweets you want retrieved as the second parameter.

[php]
<?php
/*
Parse Twitter Feeds
based on code from http://spookyismy.name/old-entries/2009/1/25/latest-twitter-update-with-phprss-part-three.html
and cache code from http://snipplr.com/view/8156/twitter-cache/
and other cache code from http://wiki.kientran.com/doku.php?id=projects:twitterbadge
*/
function parse_cache_feed($usernames, $limit) {
$username_for_feed = str_replace(" ", "+OR+from%3A", $usernames);
$feed = "http://search.twitter.com/search.atom?q=from%3A" . $username_for_feed . "&rpp=" . $limit;
$usernames_for_file = str_replace(" ", "-", $usernames);
$cache_file = dirname(__FILE__).’/cache/’ . $usernames_for_file . ‘-twitter-cache’;
$last = filemtime($cache_file);
$now = time();
$interval = 600; // ten minutes
// check the cache file
if ( !$last || (( $now – $last ) > $interval) ) {
// cache file doesn’t exist, or is old, so refresh it
$cache_rss = file_get_contents($feed);
if (!$cache_rss) {
// we didn’t get anything back from twitter
echo "<!– ERROR: Twitter feed was blank! Using cache file. –>";
} else {
// we got good results from twitter
echo "<!– SUCCESS: Twitter feed used to update cache file –>";
$cache_static = fopen($cache_file, ‘wb’);
fwrite($cache_static, serialize($cache_rss));
fclose($cache_static);
}
// read from the cache file
$rss = @unserialize(file_get_contents($cache_file));
}
else {
// cache file is fresh enough, so read from it
echo "<!– SUCCESS: Cache file was recent enough to read from –>";
$rss = @unserialize(file_get_contents($cache_file));
}
// clean up and output the twitter feed
$feed = str_replace("&amp;", "&", $rss);
$feed = str_replace("&lt;", "<", $feed);
$feed = str_replace("&gt;", ">", $feed);
$clean = explode("<entry>", $feed);
$clean = str_replace("&quot;", "’", $clean);
$clean = str_replace("&apos;", "’", $clean);
$amount = count($clean) – 1;
if ($amount) { // are there any tweets?
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_user = explode(" (", $clean_name_2[1]);
$clean_lower_user = strtolower($clean_user[0]);
$clean_uri_1 = explode("<uri>", $entry_close[0]);
$clean_uri = explode("</uri>", $clean_uri_1[1]);
$clean_time_1 = explode("<published>", $entry_close[0]);
$clean_time = explode("</published>", $clean_time_1[1]);
$unix_time = strtotime($clean_time[0]);

?>
<?php echo $clean_content[0]; ?>
<?php echo $pretty_time; ?>
<?php
}
} else { // if there aren’t any tweets
?>
Something Must Have Gone Wrong…
<?php
}
}
?>
<?php
parse_cache_feed(wpmayor, 1)
?>
[/php]

Source: WPBeginner

Alternative Method via RSS, no Caching

Another method you can use, this one involves the least amount of code you need to paste, and is very straightforward to use. In the second script call below replace your username instead of ‘wpmayor’.

Into your theme just above the closing </body> tag:

[php]
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/wpmayor.json?callback=twitterCallback2&count=5"></script>

[/php]

Into your theme where you want the tweets displayed:

[html]
<div id="twitter_div"><ul id="twitter_update_list"><li>&nbsp;</li></ul></div>
[/html]

Source: WPBeginner

If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed.

Jean Galea
Jean Galea
Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.
Recommended For You
Recommended For You
Get more expert advice like this delivered to your inbox every week.
Join thousands of designers, developers, and marketers who trust our experts to find and test the best tools and services for your website.
By signing up to our newsletter, you agree to receive electronic communications from WP Mayor that may include advertisements.
Get more expert advice like this delivered to your inbox every week.
Join thousands of designers, developers and marketers who trust our experts to find and test the best tools to build, improve, and optimize their websites.
By signing up to our newsletter, you agree to receive electronic communications from WP Mayor that may include advertisements.
Join the Conversation

6 Responses

  1. I used to have the third option working, but now for some unknown reason it doesn’t work.

    If anybody can see why I would welcome their feedback and can be contacted via my site.

    Thanks

    1. @Jay

      it is not working due to changes in Twitter API

      here is the alternative closes code

      getTwitters(‘twitter_update_list’, {
      id: ‘wpmayor’,
      count: 3,
      enableLinks: true,
      ignoreReplies: true,
      clearContents: true,
      template: ‘%text%’
      });

      1. Hi Saqib,

        Thanks very much for that, i will implement that later when i get home! Thats very kind of you, I appreciate your help!

  2. Thanks Jean. I used the third method very often but It is good to know the other methods by your post.

    What you think which one is the best ?

Leave a Reply

Your email address will not be published.

From web hosting to WooCommerce plugins and backup services, we have put together a collection of exclusive coupons and deals just for you.
WP Mayor_Icon_white

Reader Survey

Supercharge your WooCommerce store!