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.

Join the Conversation

51 Comments

  1. 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. 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. 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. Is there also a way to get the latest tweet with php code in stead of the [get_latest_tweet] shortcode?

  5. 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”

  6. 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.

  7. 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

  8. 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.

  9. 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

  10. 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

  11. 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.

  12. 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???

  13. 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!

  14. 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.

  15. 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.

  16. 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.

  17. 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();
    });
    });

  18. 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 :(

  19. 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.

  20. Hi Paul,
    I’m using your plugin to retrieve one tweet only, and I’d like that if the tweet contains an image it is also posted by the plugin.
    So far I haven’t had success doing it with the php code i found on the web.
    Could you help me out with this?
    Thanks!

  21. Our News page has recently stopped showing our Tweets and I can’t find out why. Anyone got any suggestions? I’m not a coder by the way, I usually just have to create, update and delete pages.

  22. Hi there,

    Is the a possibility to show a part of the last tweet? for example when you have: [get_latest_tweets username="me" count="1"] that shows: “word 1 word 2” to be able to show like this: [get_latest_tweets username="me" count="1" word="1"]…?

  23. Hi Paul,

    I’m getting an error as of today:
    PHP Fatal error: Call to undefined function curl_init() in E:\Domains\m\meantimecomms.com\user\htdocs\wp-content\plugins\get-latest-tweets\tmhOAuth.php on line 635

    This seems to be from the latest version of your plugin, could you help please? We’ve had to disable the plugin for now as it kills the whole site.

  24. Afternoon Paul,

    Thanks for getting back to me on this one.
    The website is on shared hosting with Fasthosts, so we don’t have any control over that.
    Do you have any ideas on why it was working yesterday then stopped suddenly today?

    We have the option to upgrade from PHP 5.4 to 5.6 but worried it will knock the WordPress site out in doing so.

  25. It sounds like they broke something on their end. Email tech support and tell them specifically about curl being missing.

    WordPress is definitely compatible with PHP 5.6 — I’m running PHP 7 now.

  26. Hi Paul,

    Thanks for your help with this, we contacted Fasthosts and they corrected the CURL issue, now our error has changed. :)
    Error: Invalid or expired token. (89)
    Could you just let me know if this is an issue with your plugin or Fasthosts once more? It will be the last time I ask…also feel free to delete my previous posts.

    Thanks again.

Leave a comment

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