Update (2013/08/03): This simple technique no longer works thanks to the 2013 Twitter API, which makes it much more complicated to grab your latest tweet. For WordPress, check out the Latest Tweets Widget.</update>

My previous theme displays my latest tweet at the top of every page. It turned out to be an excellent technique for getting more followers – visitors see the tweet, click the link, and possibly follow me on Twitter. There is even a cookie-powered “Hide” link for uninterested visitors to hide the tweet for awhile. I received quite a few requests for a tutorial on the technique, so here is how to display your latest tweet with show/hide cookies. For this tutorial, we’ll be using HTML, CSS, jQuery and WordPress. Here is a Demo of the finished product.

Overview

Once implemented, this technique will display your latest tweet as a link on your web page(s). Visitors may click on the tweet to arrive at your Twitter page, and/or click on the “Hide” link to hide latest tweets for some period of time. By default, the tweet will remain hidden for one day, or until the cookie is removed from the browser cache. We’ll see how to specify a different “hide duration” in the jQuery part of the tutorial.

Where is the latest tweet displayed on the page? That depends on the CSS. By default, the tweet will appear exactly where you put the HTML/PHP code in your WordPress theme template. Then with a few additional CSS styles, we’ll see how to position the tweet in a fixed position at the top of your pages.

Step 1: WordPress

To begin, you need WordPress and the SimpleTwitter plugin, or some other way of caching and displaying your latest tweet. The SimpleTwitter plugin is lightweight and simple to use:

Go to the SimpleTwitter Settings Page

Enter your Twitter name and cache time

Save Settings

You can use whatever cache time you like, but don’t go crazy in either direction. Base your decision on your tweet frequency. For heavy tweeters, something like 10 minutes, and for lighter tweeters maybe 120 minutes (2 hours). Once you save your settings, it’s time to add the HTML/PHP code to display the tweet on your pages.

Step 2: HTML/PHP

With the SimpleTwitter plugin configured, we now want to add the HTML and PHP code required to display your latest tweet. This code may be placed anywhere in your theme template, but is designed to be placed in the footer.php file, and then absolutely positioned with CSS to display at the top of your pages.

The only edit to make here is changing my Twitter name, “perishable”, with that of your own. This is basically just a little HTML markup with the requisite SimpleTwitter template tag. Nothing fancy. Once this code is in place, it’s time to style it up with a little CSS.

For the twitter-bird background, just grab a copy of the image appearing at the beginning of this tutorial and place it into your theme’s /images/ directory. If you’re using the default WordPress stylesheet, style.css, the background image should display just fine. If it doesn’t, make sure the image at the specified location.

Update: I changed the image path to include “http://example.com/”, so remove that bit to get just “images/twitter-bird.png” for the background url() value.</update>

Notice also that with div#tweet-link{display:none;}, we’re hiding the latest tweet by default. Once the jQuery kicks in, the tweet will be displayed, so if you don’t see it, don’t panic – we’ll get there in just a minute. The last thing to note about this CSS code is the use of proprietary browser-specific prefixes for the rounded corners on the button. Feel free to remove this fluff and restyle the “Hide” button however you wish.

Positioning the tweet: To fix the position of your tweet at the top of your web pages, two additional lines of CSS are required. First, add the following to the div#tweet-link selector:

position: fixed; width: 100%; right: 0; top: 0;

And then add this line to the div#tweet-link div selector:

text-align: right; float: right; clear: none;

Refresh your page and you should see the tweet displayed in the upper-right corner. You may need to tweak the CSS a little to get everything so arranged with your design, but the real trick here is keeping that Latest Tweet displayed at the top of the window regardless of where the user is scrolling on the page. Again, check out my previous theme to see an example of the tweet displayed with fixed positioning. I have found that displaying a persistent tweet at the top of my pages is an excellent way to attract new followers. And with the handy Hide button, everyone’s a winner.

Step 4: jQuery

Last step involves two jQuery scripts. The first part of the following code is the excellent Cookie plugin, which enables the “show/hide-cookie” part of the technique. No changes should be made to the Cookie plugin. It is included here without comments for convenience, but you can learn more here.

The second jQuery snippet displays the tweet, and then sets a “perishableLatestTweet” cookie with a “hide” value of one day. To change this to a different hide duration, edit the “1” in the line commented with “// 1 day”. Other than possibly that, nothing needs changed/edited – this code is good to go.

CSS – is the CSS included somewhere in your theme (e.g., the style.css file)?

jQuery – is the jQuery script included after jQuery?

Everything should work great, but if not check these things first. The only thing that may require some thought is fine-tuning the CSS positioning and general styling to match your design. Depending on existing styles, the CSS may require some tweaking.

Books

Links

About the site

Perishable Press is the work of Jeff Starr, professional developer, designer, author, and publisher with over 10 years of experience. Check out some of Jeff's books and projects, follow on Twitter, or learn more »

Fun fact: Perishable Press has been online since 2005, and now features over 700 articles and more than 11,000 comments. More stats »