Ottopress – A New Blog

Joseph Ducreux, original pimp.
Joseph Ducreux laughs at your nonsense.

For those of you who read this site for my WordPress knowledge, code, rants, or what have you, I’m writing this to point you in a new direction. I’ve started a new site just for that sort of thing: Otto on WordPress. Despite the name, I plan on putting other things there too, including code and other geekery.

Partially I’m doing it because I feel that I want to post more personal information type stuff here. More stuff about Memphis and what I’m up to and photo libraries and such, and my friends aren’t much into that sort of thing. Partially I’m doing it because I’d like to build more of a personal brand.

But, mostly I’m doing it because the ottopress.com domain name was available and I liked it. :)

I won’t be eliminating all geekery from this site, but it will be significantly toned down. Maybe. Dunno yet.

So, I’d suggest going over there if you like my technical rantings and ravings, since those won’t be here anymore. Also, this site may not be busy for a while. It’ll take a while to get into the swing of things, and I may start pulling more smaller microblog type posts in here. So if you want to switch your subscription around, now would be the time, while I make the changes.

For those people subscribing only to my WordPress tagged posts feed (I know there’s a few), I’ve redirected that feed now. You don’t have to switch, though you may want to. And if you suddenly got a bunch of repeat posts, that’s why. I moved a few of them over there when setting up.

So there you go.

BTW, if you’re not subscribing to my feeds, but prefer to use Facebook or Twitter, then I’ve separated some of that all out now too.

You can follow Otto on WordPress on Facebook here: http://www.facebook.com/apps/application.php?id=334947428931

You can follow this blog, Nothing to See Here, on Facebook here: http://www.facebook.com/apps/application.php?id=116002660893

And you can follow both of them on Twitter here: http://twitter.com/ottodestruct (Still working on this one, it’s not 100% reliable yet).

HOW-TO: Make WordPress Blogroll's smarter

Warning: Heavy geek content ahead. If you’re not interested in PHP code, you can safely skip this post.

One thing that not a lot of people know about is that WordPress is capable of checking your bookmarks/blogroll links for you and organizing them according to the time they were last updated.

The reasons people don’t know this are:
a) It’s not automatically setup and working, and
b) It rarely works in the first place.
Continue reading “HOW-TO: Make WordPress Blogroll's smarter”

Fun with PHP and WordPress

Guy over here asked how to display hockey scores on his blog. Here?s my solution?

Edit: Fixed a minor problem with the regular expression. Some of the soccer team names caused problems on that feed.

A quick googling tells me that exactscores.com seems to have feeds for scores for lots of stuff, however they also put horrible amounts of advertising into the RSS feed. Nevertheless, that can be worked around, if you want. )

The actual score information in the feeds comes in the titles, and it looks like this:

(No)001/1 (BST+2h,Start at)03:00 (Home Team)EDM OILERS (Final)3-6 (Away Team)ANA MIGHTY DUCKS (Status)Finished (First Period)0-3 (Second Period)3-2 (Third Period)0-1 (Full Time)3-6 (Over Time)- (Penalties)- (Scorers)LUPUL(19)

Now, that is pretty ugly. But, at least it?s in a semi normalized format: (Key)Value? The Scorers at the end isn?t pretty, but we can deal with that later. Also, thankfully, the actual scores in all the ones I checked always start with (No), so we can use that to determine which bits of the feed are actual scores and which are not.

So here?s a simple script to retrieve a feed and then display only those bits that start with (No):

< ?php
require_once(ABSPATH . WPINC . '/rss-functions.php');
$rss = fetch_rss('http://www.exactscores.com/HockeyLivescoreRss.xml');
echo '
    '; foreach ($rss->items as $item ) { if (preg_match('/^(No).*/i',$item['title'])) echo '
  • '.$item['title'].'
  • '; } echo ''; ?>

Obviously, the output of this is ugly, but it proves that we?re only getting scores out of the feed.

So, now we need to parse that string and get more useful data out of it. Here?s where preg_split and some rather disturbing regular expression syntax is your friend?

$uglyscores = preg_split('/^(([^)]*))[^s]|s(([^)]*))[^s]/',
	$item['title'], -1, PREG_SPLIT_DELIM_CAPTURE);

I called it $uglyscores for a reason. This returns an array with the bits of the scores nicely parsed out, but because of the regular expression I used, there?s a blank string between each pair of data, so let?s reformat this mess into something nicer:

$i=1; // skip the first element
$scores = array();
while ($i<count ) {
	$scores[$uglyscores[$i]]=$uglyscores[$i+1];
	$i+=3; // skip the blank separator
}

This gives us a nice array of elements that we can then reference by the key names. So we can use stuff like $scores[?Final?] and such.

So now that we don?t have to display everything, let?s put it all together. Here?s a combination of all of the above that outputs the two teams and the final score only:

< ?php
require_once(ABSPATH . WPINC . '/rss-functions.php');
$rss = fetch_rss('http://www.exactscores.com/HockeyLivescoreRss.xml');
echo '
    '; foreach ($rss->items as $item ) { if (preg_match('/^(No).*/i',$item['title'])) { echo '
  • '; $uglyscores = preg_split('/^(([^)]*))[^s]|s(([^)]*))[^s]/', $item['title'],-1,PREG_SPLIT_DELIM_CAPTURE); $i=1; // skip the first element $scores = array(); while ($i<count ) { $scores[$uglyscores[$i]]=$uglyscores[$i+1]; $i+=3; // skip the blank separator } echo "Home Team: ".$scores["Home Team"].""; echo "Away Team: ".$scores["Away Team"].""; echo "Final Score: ".$scores["Final"]; echo '
  • '; } } echo ''; ?>

And there you have it. If you want to display different information, just modify the stuff in those echo lines towards the end.

The key is finding the RSS feed with the data you?re interested in. Once you have that, it?s usually fairly straightforward to parse it and redisplay it nicely. )