Fun With WordPress Theme Toolkit

I came across WordPress Theme Toolkit while developing my current theme; Tuned… and I thought, what the heck, I might as well give it a whirl.

I’d like to share one of the things I use the toolkit for: Automatically Generating Feeds Links. To implement this project on your WordPress powered blog, you’ll need the feedListplugin, and WordPress Theme Toolkit (of course).

Setting up these plugins are not in the scope of this mini tutorial. If you have problems doing that, then I guess this tutorial is really not for you, yet. No that that’s out of the way… let’s get cracking!

Getting Input

First off, we need some way to get some input to start displaying the feeds. At the very least, feedList needs the feed URL to work. Other info you’ll need to generate a well-structured feeds list are as follows:

  1. A descriptive title for the feed.
  2. URL of the site you’re syndicating from.
  3. How many items do you want to display.

Now, let’s get this project up and running. Create a textarea in your theme’s functions.phpwhere we will enter the items above (refer here for instructions). Mine looks something like this:

'feedslist' => 'Feeds List {textarea|6|50} ## Format: Site Name, http://site.url, http://feed.url, Limit',

As you can see from my example, I want to enter the data in a comma-separated list.

Data Entry

Once you have successfully created the input section, it’s time to populate the textarea. The format you should use is as follows:

Site Name, Site URL (must be full URL, with http://), Feed URL (must be full URL, with http://), Limit (How many items to show, must be an integer)

Each entry must be on one line (input line, not the wrapped line, meaning, don’t press Enter before keying in the limit).

My set of lists looks like this:

LQ Slackware Forum,http://www.linuxquestions.org/questions/forumdisplay.php?s=&forumid=14, http://www.linuxquestions.org/syndicate/slackware.xml, 5
Kernel Releases, http://www.kernel.org, http://kernel.org/kdist/rss.xml, 10
Recent Diggs, http://digg.com, http://digg.com/rss/indexdig.xml,5
My Diggs, http://digg.com/users/Azmeen,http://digg.com/rss/Azmeen/index2.xml,5

Actually Doing Something with the Data

We’ve created the textarea for input and we’ve actually populated it with data. Now let’s turn that data into something useful… you know like, actually generating the feeds!

Now, this requires a little bit of PHP programming. We need to write a function that will automatically output the feeds list on our web page(s). This function should be created near the end of your functions.php, right before the PHP close tag ( ?> ). Fortunately for non-coders, I have already created the function for you to use:

function getfeeds() {
global $mytheme;
$variable = trim($mytheme->option['feedslist']);
$array = explode("\r\n", $variable);
$subarray = array();
foreach ($array as $value) {
$subarray[] = explode(",", $value);
}
foreach ($subarray as $key => $val) {
$val[0] = trim($val[0]);
$val[1] = trim($val[1]);
$val[2] = trim($val[2]);
$val[3] = trim($val[3]);
$val[0] = str_replace("&", "&", $val[0]);
$val[1] = str_replace("&", "&", $val[1]);
echo "<li><h2><a href='".$val[1]."'>".$val[0]."</a></h2>\n";
echo "<ul>\n";
rssLinkList($val[2], $val[3], false);
echo "\n</ul>\n</li>\n";
}
}

Note that the variable $mytheme that I globalised in the function may not be the same as what you’re using. $mytheme is the default name used in WordPress Theme Toolkit, you should definitely change this to your own variable name.

What this function does is to output your feeds in an unordered list. This function will produce HTML compliant codes when placed between <ol> or <ul> tags (commonly used in WP themes’ sidebars).

Implementation

All you need to do to get your feeds list to be displayed is to call the getfeeds(); function where you want it to be displayed.

I called it in my theme’s sidebar.php. You can output your list anywhere in your theme, but remember to enclose it between <ol> or <ul> tags. You may need to modify the function a bit if you want to add extra stuff like CSS.

Caveats

The function will definitely break if the site’s feed URL or home page URL contains commas. You might want to use a different separator character if this is the case. You should also modify the getfeeds function accordingly to reflect this.

Leave a Reply