How to display tweets on a WordPress page

There are a lot of ways to display your tweets on your blog. Many are complicated and ugly. Sometimes, you hit Twitter’s API rate limit. Most require the client to perform additional work. I made a very simple WordPress plugin to handle this.

All you do is stick [get_latest_tweets username="me"] where you want the tweets to go. You can optionally supply a count=X parameter to display more or fewer tweets.

JSON is cached, preventing the Twitter server from being hit more than twice in one minute.

You get markup like this, which you can style as you please:

<ul class='tweets'>
<li>@<a class='atreply' href='http://twitter.com/jane'>jane</a> Please dance a jig. <span class='date'><a href='http://twitter.com/me/status/2345'>3 hours ago</a></span></li>
<li>Anyone used TotalFinder? <a href='http://t.co/blah'>http://t.co/blah</a> Saw it on @<a class='atreply' href='http://twitter.com/bob'>bob</a>'s machine and am intrigued. <span class='date'><a href='http://twitter.com/me/status/1234'>6 hours ago</a></span></li>
</ul>

Grab the plugin from GitHub or the WordPress plugin directory.

41 Responses to How to display tweets on a WordPress page

  1. Wouter says:

    Thanks for the plugin! I changed $tweet["id"] to $tweet["id_str"], otherwise the links to the statuses weren’t correct (in the get_latest_tweets_html function).

  2. Rick Dijs says:

    Hi, I like your plugin a lot.
    (First i used embed RSS to display tweets on a page, it didn’t look good)

    But I still have a question:
    Is it possible to show more than 5 latest tweets?

    Thanks!

  3. JJ says:

    Hi!

    Great plugin, but: I got a problem with German Umlauts like äüö, they aren’t displayed correctly (“f�@r” instead of “für”).
    Is there any solution for this?

    Thanks a lot in advance!

  4. JJ says:

    And what I forgot to mention: Yes, I’m using UTF-8 on my site. ;)

  5. Adib says:

    How do I remove the bullets?

  6. Steve says:

    Is there also a way to get the latest tweet with php code in stead of the [get_latest_tweet] shortcode?

  7. The WordPress function do_shortcode() should do what you want.

  8. Steve says:

    Nice, that worked. Thanks!

  9. Dyke says:

    I get this message but I’m sure the path and file is 777 writable.

    “Could not write to JSON cache. Make sure … is writeable by the web server”

  10. Make sure the parent directories are executable by the web server.

  11. Chris says:

    I’m getting the same error, it just started a few days ago (username removed):

    Warning: file_get_contents(http://twitter.com/status/user_timeline/username.json) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home/charlott/public_html/wordpress/wp-content/plugins/get-latest-tweets/get-latest-tweets.php on line 72
    Could not write to JSON cache. Make sure /home/charlott/public_html/wordpress/wp-content/cache/latest_tweets/ is writeable by the web server

    I have triple checked permissions and everything is dandy – the cache and plugin directories both have appropriate permissions. I suspect this is the result of some change to Twitter’s API that broke your plugin. If I can figure out what the issue is I’ll follow up. It might be a rate limiting thing since it seems to work occasionally but I get that error 90% of the time over the past couple days.

  12. AO says:

    Hi Paul,

    I’m getting same error as Chris.

    Warning: file_get_contents(http://twitter.com/status/user_timeline/username.json) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home/charlott/public_html/wordpress/wp-content/plugins/get-latest-tweets/get-latest-tweets.php on line 72
    Could not write to JSON cache. Make sure /home/charlott/public_html/wordpress/wp-content/cache/latest_tweets/ is writeable by the web server

    You mentioned “to make sure the parent directories are executable by the web server?”. How can I do that?

    Thanks

  13. You’ll need to chmod o+x the parent directories.

  14. The function to parse JSON in /status/user_timelime/username.json has been deprecated. You will need to change it to:

    http://api.twitter.com/1/statuses/user_timeline.json?screen_name=username&include_rts=1

    This should fix the Could not write to JSON cache error (which is actually induced by a 401 response from twitter because of the deprecated API call)

    Best regards,

  15. Tim Man says:

    I am getting this same error “Could not write to JSON cache. Make sure … is writeable by the web server” all directories confirmed to be writable (wasn’t nothing changed to not make them writable). This just started happening a week or so ago.

    Also tried Paulschreiber’s suggestion of chmoding 0+x, didn’t help either.

    Totally out of ideas now. Any help would be appreciated.

  16. NICK WHITE says:

    Daniel,

    Swapping out the API call didn’t work for me (I’m getting the same error as you and Tim Man). Would you please be a bit more specific about what I need to do to get things back up and running?

    Thanks,
    Nick

  17. Jason Curtis says:

    Hi,

    First of all, love the plug-in. Secondly, is there a way to remove (or not show) the replied tweets?

  18. Jason: no — you’d have to fork it and add that option yourself.

  19. Hamish says:

    Hi there Paul,
    Thanks for the plugin,
    I tried your suggestion re: the address change and our site is still having issues. The perms on the folder are open.
    Can you please advise what could be going wrong when you get a chance?
    Thanks!
    Hamish

  20. Make sure that’s letter “o” +x and not a zero.

  21. Filipe says:

    Hi, Paul. I’m having the same problem related above:
    Warning: file_get_contents(http://twitter.com/status/user_timeline/username.json) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home/charlott/public_html/wordpress/wp-content/plugins/get-latest-tweets/get-latest-tweets.php on line 72
    Could not write to JSON cache. Make sure /home/charlott/public_html/wordpress/wp-content/cache/latest_tweets/ is writeable by the web server

    I tried to change the code with “curl_setopt” and it works partially. Sometimes it returns “/ R” and others it works.

  22. // add @reply links
    $tweet_text = preg_replace(“/\B[@ļ¼ ]([a-zA-Z0-9_]{1,20})/iu”,
    “@$1“,
    $tweet);

  23. That was solution for JJ problem with “äüö” also in latvian for “š” and “ļ”.

  24. Nathan says:

    Hey Paul,
    any word on the JSON cache error? it would be really helpful to find a fix.
    Thanks

  25. It sounds like a configuration / permissions problem on your server.

  26. Jon Bolden says:

    I went to the github page and pulled the newest version and my JSON cache error went away:
    https://github.com/paulschreiber/get_latest_tweets

  27. Gene says:

    I’m also have a problem with the JSON error… tried using the changes suggested by motoxer4533 from the WP support forum but it resulted in displaying something strange and not really working quite right… The version number in github is 0.1.1 but the version in the WordPress repository is 0.1.3, not sure which one is the newest but it seems to have been updated around the same time frame… tried the one on github but it still doesn’t work. I definitely have the permissions on 777 for the cache folder and the latest_tweets folder… anyone suggestions or ideas???

  28. 0.1.3 is the latest. I’ve updated GitHub.

  29. Alex C says:

    Hi,

    Great plugin – I’ve got everything working but can’t workout how to add the tweet count to the shortcode referenced here ( You can optionally supply a count=X parameter to display more or fewer tweets.) I’ve tried a few ways such as [get_latest_tweets username="aeonmag",count=1] but not found the one that works.

    Could you show me how this is done? Thanks!

  30. Parameters should be space-separated: [get_latest_tweets username="aeonmag" count=1]

  31. George Park says:

    Thanks for the plugin.

    I too would like to know how to remove the bullets.

    Also, how does one centre the Tweet?

    I have centered the code in the page, but it still aligns to the left.

  32. You’d need to use the appropriate CSS, i.e. ul.tweets { list-style: none; }. Google “CSS tutorial.”

  33. George Park says:

    Thanks for that Paul.

    Have tried to edit the CSS, but did not work.

    So, am wondering if there is a change that can be made in the php?

  34. You’ll have to get someone to help you with the CSS. Perhaps you’re using the wrong selector or your rule is being overridden by a later or higher-priority rule.

  35. George Park says:

    Thanks, will have to look deeper into it.

  36. George Park says:

    Right, the way I got this to work for me is as follows:

    #content ul.tweets li {
    list-style-type: none;
    }

    Had to add in #content and now the bullet is gone.

    Hope this will help someone else.

  37. George Park says:

    I have another question.

    How can I change the clickable link to open in a new window?
    At the moment, the Twitter page opens and takes you away from the original page.

    I am not sure if this is a php change or a css change.

  38. George: what you’re asking has nothing to do with PHP, CSS or WordPress. You should ask general programming qustions on stackoverflow.com.

    One way of doing what you want is to use jQuery:

    $(document).ready(function () {
    $(“ul.tweets a”).click(function(e) {
    window.open($(this).attr(“href”));
    e.preventDefault();
    });
    });

  39. George Park says:

    Thanks Paul.

    Forgive the questions I ask, as I am really ignorant when it comes to any code and what it does.

    Still learning :)

  40. Ralph says:

    I’ve been fighting with this plugin for over 2 hours now. It used to work just fine and it will just stop working out of the blue. I have even chmod 777 the latest_tweets folder which is in my opinion not necessary and very insecure. I really don’t know where else to look for a fix. I can’t comprehend how it is possible for it to work and then just break on its own. not good :(

  41. Tinsley says:

    If you’re getting the ‘Could not write to JSON cache.’ and you have already checked your server permissions chances are the problem is not with you. The plugin uses the result of file_put_contents to determine whether or not to display that error message but doesn’t take into account that file_put_contents can return 0 or false. If the initial request fails then file_put_contents will return 0.

    Using curl in place of file_get_contents may fix the issue. I was able to fix it by setting file_get_contents to ignore errors:

    function get_json_from_twitter($username, $count) {
    $url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username;
    $opts = array('http' =>
    array(
    'method' => 'GET',
    'ignore_errors' => '1'
    )
    );
    $json = file_get_contents($url, false, stream_context_create($opts));
    return $json;
    }

    Hope that helps.

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>