With the inception of this blog I decided to play with a new theme.
I chose the MinaFlow theme by Micheal Kelsinger I’ve used his free themes before and their pretty good. Once installed and after making some changes to the CSS and Layout as I wanted it as a two column theme rather than a three column, I came to installing the widgets on the sidebar. In the previous theme I had used it had come with a bespoke Twitter widget for displaying your latest Tweets but I discovered that this won didn’t. Rather than install a widget created by other people I set about creating my own widget to display the Tweets.

I’d helped Aaron to amend his header.php file in his theme to display his latest Tweet as my theme did and so I used that code as a basis for my plugin. The code for getting your latest Tweet can be found @designwoop.com written by Stu Greenham.

A WordPress Plugin/Widget is a PHP file at heart using some WordPress functions to integrate it into your blog and PHP to do the actions.

We’ll start with the header of the file. The commented code below must be inserted into your main .php file as it is used to tell WordPress about the plugin. The items are self explanitary and can be seen in use in the Plugins dashboard menu.

Plugin Name: Tweetr
Plugin URI: http://www.robmcghee.com/tweetr/
Description: Display the latest tweets from a Twitter account
Author: Rob McGhee
Version: 1.0<br />
Author URI: http://www.llygoden.com/

The first line of code we need to add to our plugin is the add_action, this line of code tells WordPress to load the plugin and tells it to look at the first function. The first function call we need to make is the <plugin name>_init() call to initialize the plugin settings. Inside our _init() function we tell the plugin that we’re a sidebar widget and point it to the widget function and we also tell the plugin that we have a control page and again point it at the control page function.

add_action("plugins_loaded", "tweeter_init");
function tweeter_init(){
register_sidebar_widget("Tweetr", "widget_tweeter");
register_widget_control("Tweetr", "tweeter_control", 300, 200);

The second function call we need to make is the widget_<widget name>($args) call to setup the widget. The function below is used to keep the widget in sync with the pages theme, it also sets the title for the widget and then calls our main function tweeter().

function widget_tweeter($args){
echo $before_widget;
echo $before_title;?>Tweetr<?php echo $after_title;
echo $after_widget;

The third function that we call is <widget name>() the code in this function is what our widget is does so in my example it queries Twitter for your last Tweets and displays them.

function tweeter() {
$options = get_option("widget_tweeter"); //get the options setup for the widgit
$username = $options['tUser']; // Your twitter username
$tweetnum = $options['tNum']; // Number of tweets you want to fetch
$i = 1; // i is set to 1
$feed = file_get_contents("http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $tweetnum); // get the twitter feed
$stepOne = explode("<content type=\"html\">", $feed);
while ($i < count($stepOne)){ // we set i to start at one to miss the first line of junk
$stepTwo = explode("</content>", $stepOne[$i]);
$tweet = $stepTwo[0];
$tweet = str_replace("&lt;", "<", $tweet); $tweet = str_replace("&gt;", ">", $tweet);
$tweet = str_replace("&amp;", "&", $tweet);
$tweet = str_replace("&apos;", "&#39;", $tweet);
$tweet = str_replace("&quot;", "", $tweet); //blank the quote
$tweet = str_replace("#fb", "", $tweet);
$tweet = str_replace("&#34;", "", $tweet); // just in case the other type crops up
if ($tweet != ""){ // stop a blank line appearing first off
if ($i > 1){
echo "<br />"; // put a gap between items
echo $tweet . "<br />";
$i++; // increase i to fetch the next tweet

The final function that we call is <widget namecontrol() this function describes and sets up our control panel for settings in our widget.  We use get_option to retrieve any settings saved for the widget, we then call !is_array($options) to see if there are any settings saved, if there aren’t then we put some default settings in. We create a form using HTML to produce the text boxes and other options we want for saving our settings.

function tweeter_control(){
$options = get_option("widget_tweeter");
if (!is_array( $options )){
$options = array(
'tUser' => 'llygoden',
'tNum' => '1');
if ($_POST['tweeter-Submit']){<
$options['tUser'] = htmlspecialchars($_POST['tweeter-tUser']);
$options['tNum'] = htmlspecialchars($_POST['tweeter-tNum']);
update_option("widget_tweeter", $options);

<label for="tweeter-tUser">Twitter Username: </label>
<input type="text" id="tweeter-tUser" name="tweeter-tUser" value="<?php echo $options['tUser'];?>" />

<label for="tweeter-tNum">Number of Tweets to pull: </label>
<input type="text" id="tweeter-tNum" name="tweeter-tNum" value="<?php echo $options['tNum']; ?>" />
<input type="hidden" id="tweeter-Submit" name="tweeter-Submit" value="1" />


The final widget can be seen on the right hand side of this screen and the control panel is below:

Tweetr: Control Panel