“Display Tweets” Plugin

April 5, 2013 — 28 Comments

"Display Tweets" Plugin

Recently there has been a lot of controversy over the Twitter API changes and restrictions that have been put in place. This is going to create an added level of difficult to simply displaying a Twitter feed on your WordPress site.

Unfortunately, the traditional client-to-server approach (fetching tweets using Javascript) is no longer viable. Instead, developers are being forced to use a server-to-server approach, such as with PHP. This is because Twitter API requests now require authentication. In-fact, we need four alpha-numerical keys just to get access.

Personally, I see this becoming a real pain. Many of the current Twitter-related WordPress plugins will become defunct when Twitter shut-down their old v1 API for good, with Twitter feeds disappearing. One can anticipate that there will be some worrying errors thrown by scripts too. The access restrictions won’t do any favours to sites with higher traffic either.

So what do we do?

Well, it’s still possible, just a little more difficult. Fortunately, I’ve set out to make this one step easier with the release of my “Display Tweets” plugin.

This plugin is very straight forward and is specifically designed with theme and plugin developers in mind. It doesn’t load any styling or Javascript. Instead, it fetches your tweets via PHP (making requests to the new API so it’s future proof) and prints them in plain text.

Your tweets are then cached within WordPress, keeping your Twitter API requests to a minimum. After 5 minutes your tweets are refreshed and cached again. Simple, and trouble free. As everything is server-side, there’s no Javascript either. This keeps network requests to a minimum.

Many WordPress hooks have been included to make overwriting the printed tweet template easy. This is perfect for theme integration where complete control over the HTML is required. Simply add the code below to your theme’s functions.php file.

function custom_tweet_template( $tweet ) {
    .. Print your custom tweet HTML here ..
}
add_action( 'displaytweets_tweet_template', 'custom_tweet_template' );

It is as simple as that!

I’m really hoping to turn this plugin into a community driven effort, so I’ve uploaded it to GitHub. Feel free to add to it or fix any bugs you come across.

Github Download

28 responses to “Display Tweets” Plugin

  1. Firstly Thank You for this future proof plug-in…I am fairly new to WP and want to know where I can put css styling code to customize the look? Is it entered in the function function custom_tweet_template and then that block of code is amended to the end of the functions.php code as directed. Where can I find the displaytweets_tweet_template coding.
    Thank you.

    • It would probably be best to add your CSS styling code to your theme’s style.css file. That way when the plugin is upgraded in the future your changes won’t be lost. Styling it using CSS doesn’t require using any PHP to alter the plugin at all; this is only required if you want to alter the HTML markup of each tweet.

      • Can an example be shown what/where class/style tags be placed?

        I see this line –
        echo “{$this->format_tweet( $tweet->text )} {$posted_since}”;

        But how do you style that?

        Thanks

        • No, you should style the HTML outputted by the plugin instead. Just like you would style any other HTML by adding CSS to your theme’s style.css file. You can’t style PHP.

          For targetting the tweets, put the shortcode inside something like “<div class=”tweets”>” and “</div>”, then style “.tweets” and its corresponding children.

          For doing things like this I’d definitely recommend having some advanced CSS and HTML knowledge. Unless you want to override some of the plugin functionality, PHP knowledge shouldn’t be needed.

  2. Hi, Matt. I read in the info above that the tweets are cached to keep network requests to a minimum and I understood the actual API call, refresh interval to be every 5 minutes. It does not appear to be the refreshing that frequently for me. Not really a big deal, because it is not failing to refresh entirely or anything. Hooray for client nitpicking. As if we all don’t have better things to do than investigate why his latest tweet didn’t show up within 5 minutes of being posted. :D

    • Hey Dave, good to hear from you. What kind of interval are you experiencing?

      • Hi, Matt. Thanks for the response. The latest tweet is displaying right now so everything is up to date. I’m not really sure when that refresh occurred, but I know that it had gone at least 3 hours without displaying the latest tweet earlier today. Before that, it had been several days according to the client… although I’m not convinced of the accuracy of that report. I will keep an eye on it and come back to update you after I get a more accurate sense of the timing. I understood the plugin to have a refresh interval of 5 minutes by design and I wanted to confirm that for starters. Are there any circumstances that you have encountered that might delay that refresh from occurring when the plugin is working normally otherwise? Thanks!

        -Dave

        • It’s possible that I may have incorrectly set the refresh interval in v1.0. Since leaving that comment I’ve looked into the code and released v1.0.1 with some alterations, so try updating the plugin and giving it a try. The same update also has an added check and will only load the TwitterOAuth class if not loaded already (in response to a topic you posted on the wordpress.org forums).

          • I went ahead and updated the plugin but it did not immediately resolve the issue (the last time v1.0 had updated the twitter feed was Monday). After I updated to v1.0.1, the tweets were still not up to date. I had previously set the feed to only display 4 tweets, and I changed it to 5. The plugin immediately updated the feed after saving that setting. I am wondering if the bug might show up after setting it to display a # of tweets less than 5? Or, it could be that changing something with the settings caused the plugin to make a new API call. Not sure at this point. I will leave it at 5 for a while, and see if it stays up to date. If it does, I will then decrease it to 4 again and see if it falls out of date. I will report back to you with my findings. Thanks, Matt.
            -Dave

          • Probably just a case of the old cache not getting updated until you re-saved the settings. Hopefully should be fine from now on :)

          • Hey, Matt. It has been working perfectly since updating. I switched it back to 4 tweets in the feed. I expect it will be fine as well. Thanks for the updated version!

            -Dave

          • Awesome! Great to hear :)

  3. Dear Matthew
    I am totally new to WP and this evening only just starting to play with layout and content. The aim being to replace our existing HTML with a WP driven site. Having a Twitter feed is key to the members usability of the site, so it is virtually the first thing I wanted to look into.
    I am almost embarrassed to ask, but how do I configure your Plug-in with our twitter account name
    Kind regards
    Nigel

    • Hi Nigel, unfortunately it isn’t as simple as before with the new Twitter v1.1 API. To display your feed you need to register an application with Twitter, then enter the required API information into the plugin’s settings.

      The “Display Tweets” plugin settings can be found in the “Settings” admin panel. Have a look at the panel and read the brief instructions on getting setup. Should help guide you in the right direction.

  4. Sven Schneider June 10, 2013 at 4:08 pm

    Hi, I authorized your plugin and saved everything. However, when I put the widget in my footer I’m getting the following error. I’m using WordPress 3.5.1

    Fatal error: Cannot redeclare class OAuthException in……/wp-content/plugins/display-tweets-php/includes/Twitter/twitteroauth/OAuth.php on line 8

    • Sven Schneider June 10, 2013 at 4:26 pm

      hm .. it seems almost every plugin does generate this error in my installation, even with the default themes ..

      • Sven Schneider June 10, 2013 at 4:48 pm

        fixed it by commenting out Line 6 in Oauth.php:

        // class OAuthException extends Exception {
        // pass
        // }

        • Hi Sven, sounds like your theme and/or another plugin already had a class with the same name as OAuthException, thus generating errors. I’ll have a look during the next update to see if I can fix this :)

  5. Matthew,
    With twitter having finally gone to API v1.1, the previous theme-based widget I used to populate tweets on my site is no longer functioning. The theme developer pointed users to your plugin, and I’m really glad to have something that works. I’m a complete novice (I’ve literally learned bits and pieces of html and css as I’ve needed to to build the site), and I’m wondering if you can point me in the right direction to make two slight modifications. First, how can I add space between each of the tweets as they appear on the site. Second, is it possible to amend the timestamp so that instead of the exact date and time, it says something like ’3 hours ago’ or ’1 day ago’? If so, how would I go about it? Thanks so much!
    John

    • Hi John, unfortunately this plugin is really geared towards developers who want to integrate the new API with their theme’s and/or plugins. Hopefully you’ll not find it too difficult however :)

      The first modification would probably require you to add some custom CSS. If you’re familiar with CSS, this should be simple, otherwise you may struggle a bit. I’d definitely giving it a try – CSS knowledge will prove invaluable throughout running a WordPress site.

      The second modification will require some PHP code – you’d need to hook into the plugin and use your own methods to manipulate the datetime stamp. Unfortunately this is a much more tricky modification and not very straightforward. Again, I’d definitely recommend giving it a go and attempting to learn, but it’s certainly not an easy one I’m afraid :(

      Hope this helps!

  6. Hey there, thanks very much for this great plugin. Can’t believe they decided to retire API v1.0. :(

    Just a small suggestion – It would be good if you could change the date string from the backend. I had to edit displaytweets.php to display a date format that I liked.

    Cheers.

    • To keep the plugin lightweight and focussed I’ve decided not to include any backend options like this – instead the developer is given full control using PHP.

      I’ll definitely have a think about this however! :)

  7. Hi Mathew Ruddy

    After following all the steps to configure this plugin I’m little confused, if i add piece of code in function.php as you mentioned, the parse error is coming on the website.

    I’m using Striking theme for my WP website.

    Thanks

    Best
    Nandakumar

    • Hi Nandakumar, if you don’t need your own custom tweet output HTML then you shouldn’t need to insert the code into your functions.php file. If you do, ensure if it inserted after the opening ‘< ?php' at the top of the file, and before the '?>‘ at the bottom. Also ensure you are typing the code yourself to avoid any formatting errors that may occur from copy and pasting.

  8. Hello, just discovered your plugin via http://www.freshconsulting.com/how-to-fix-your-broken-twitter-stream-widget-on-wordpress/, it’s a great job and worked mostly fine.
    Only one issue, it seems that you’re using curl directly to communicate with twitter instead of the own worpress http-api (http://planetozh.com/blog/2009/08/how-to-make-http-requests-with-wordpress/)
    The full reference is here, http://codex.wordpress.org/HTTP_API
    Our only glitch is with the use of proxy which would be transparent if the class was used…
    Would you accept a patch for this?

    • The class used for querying the Twitter API is just a package actively maintained by another PHP developer. Didn’t write it myself, and it’s not WordPress specific. Feel free to push changes to Github if you decide to patch it :)

  9. Getting this on my demo site:

    Warning: Invalid argument supplied for foreach() in /home/shoppico/public_html/brands/wp-content/plugins/displaytweets-master/displaytweets.php on line 591

    Just wondering if you know what this means?
    url: http://shoppi.com.au/brands/demo/

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>