Primarily the focus on this tutorial is to display your cached tweets using PHP and OAuth on your WordPress website. We will be accessing the secure Twitter 1.1 API with PHP by parsing and caching the Json file using the WordPress API. It may seem a lot of code just to display your latest tweets on your website but the benefits are that you can style it to the taste of your theme with a little CSS and caching the Json file will not add extra load on the Twitter API.

New Update. I have packaged all of the features and more into a WP plugin. Please remember to remove any of the code used from this post before installing the plugin. Check out WOW-Twitter.

Prequisites

  • You should already have a consumer key, consumer secret, access token and access token secret.
    If not please go to http://www.worldoweb.co.uk/2012/create-a-twitter-app-for-the-new-api for a little guide into creating the Twitter App required for this tutorial.
  • PHP Server with CURL – I have tested it using PHP 5.3+
  • A little knowledge of PHP, CSS and how the WP themes work are beneficial but not essential.
  • This tutorial uses the tmhOAth script by Matt Harris, included in the source files.

Download the Source Files

Last Updated:2nd July 2013
Download the source files from below:

Download Display Tweets

Unzip and copy the following files to WordPress Theme or WordPress Child Theme directory.

  • tmhOAuth.php
  • cacert.pem

tmhOAuth.php is a PHP authentication script to allow you to make a secure call to the twitter API.
cacert.pem enables you to access the twitter API using SSL. You can also download a copy from the following: http://curl.haxx.se/ca/cacert.pem

Adding the Function to WordPress

Now you need to add the following function to your functions.php which you will find in your theme directory. If you haven’t got one you can easily create one using a IDE or your file manager.

Lines 13-16 you need to add your consumer key, consumer secret, access token and access token secret that you saved. There is more detailed explanation of the function in the source code.

Calling the function

Now you need to call the function from your theme to display your tweets. You can place it in your footer or in your sidebar. If you want to place it in a widget or blog post you will have to install a plugin that executes PHP but bear in mind that I haven’t tested this method yet!

The display_tweets function takes 4 parameters, examples below.

$style

(String) Optional Displays the date or time since format. Leave string empty if not wanting to use a date.
The arguments are:

  • ‘eng_suff’ – Displays 6th November
  • ‘ddmm’ – Displays 06 Nov
  • ‘ddmmyy’ – Displays 06 Nov 2012
  • ‘full_date’ – Displays Tues 06 Nov 2012
  • ‘time_since’ – Displays the time since the tweet in hours, minutes etc.
  • ‘default’ – Displays November 06 2012

$twitter_id

(String) Required Your Twitter screen name (username)

$max_tweets

(int) Optional How many tweets you want to display. The default is 10.

$is_child

(boolean) Optional If your theme is a child theme then you want to set it to true. The default is false.

Examples

The default is

If this doesn’t work try adding the true value for a child theme.

Notice that I have set $max_tweets to 10. This is because function parameters are processed in order. Missing the 3rd value out would result in a error.

To display 20 tweets with the time since and your theme uses a child theme.

Styling our Tweets

The output will display a Unordered list which you can style easily using CSS by opening up your theme’s style.css. Here is the CSS that I use to display my tweets in my footer. You can easily change it to suit your requirements.

Conclusion

If you are pretty new to PHP then you may want to stick with the defaults then use it as a basis to learn. If you have any problems or recommendations or tweaks I would love to hear them.