Programming is a wonderful mix of art and science; source code is both a poem and a math problem. It should be as simple and elegant as it is functional and fast. This blog is about that (along with whatever else I feel like writing about).

Sunday, August 31, 2008

The Contractor Experiment Ends, Plus My First Wordpress Plugin

It's been a while since I've posted anything, but I figured I should mention this: The Contractor Experiment is over, after just one contract. I found myself in a place I liked, with a good working environment and good co-workers ... and I became a full time employee last week. I plan on working exactly the same way as an employee as I did when I was a contractor -- why else would they have hired me? -- and it should remain a lot of fun.

Meanwhile, I think I should also mention a little side project I've been working on, and have just released. It's a Wordpress plugin that downloads baseball player's stats and displays them on your blog.

It can be seen in action at my baseball blog, Fire Gardy. To learn more about the program, you can visit the Fire Gardy Stat Grabber home page. Or just jump straight to the project page at Github. (It's GPL software.)

When I wrote it, the first step was to write a Baseball-Reference.com parser, which grabs the requested player's page (via PHP's CURL), and runs through it line by line to get the player's batting statistics (pitching stats will have to be added later).

Once that was there, it was time to turn it into a Wordpress plugin. It was actually a lot easier than I thought it'd be.

First you go to your blog's wp-content/plugins directory. Create a new directory inside it for your plugin (mine was called "fire-gardy-stat-grabber"). Inside that new directory, create a PHP file with the same name as the plugin directory ("fire-gardy-stat-grabber.php").

Open it up and put a comment inside it that will tell Wordpress the details of your program. Mine looks like this:
/*
Plugin Name: FireGardy Stat Grabber
Plugin URI: http://stat-grabber.firegardy.com
Description: Download a player's stats from Baseball-Reference.com and display them on the page.
Version: 0.1a
Author: Sean Schulte
Author URI: http://seancode.blogspot.com
*/
Really, only the "Plugin Name" line is required.

You need to define a function that will display what you want -- because for this plugin we'll be using a "Template Tag" (which allows us to put it anywhere on the page by editing the theme's template files).

It's a simple PHP function, that can take any parameters you want. It doesn't return anything, and needs to echo your desired HTML output to the screen. Mine has the following signature:
function fire_gardy_stat_grabber($playerName, $playerId, $year)
I'm not going to print all the code for it here -- you can go check it out at Github.

You can use the Wordpress database in this function -- which is useful for caching the results you get back from Baseball-Reference. There are two options for using the database. One is to create your own tables and use actual SQL queries; that'd be useful for more complicated plugins. The other is to use the Wordpress options table which stores anything -- accessible as part of a simple name-value pair.
get_option($key);
delete_option($key);
update_option($key, $value);
The plugin also uses a Wordpress Plugin hook to load some CSS. A function like this writes the CSS:
function fg_baseball_reference_css()
And this line makes it load:
add_action('wp_footer', 'fg_baseball_reference_css');
Once you've got that in place, just toss it into your sidebar.php in your theme's template:
<?php fire_gardy_stat_grabber('Nick Punto', 'puntoni01', 2008); ?>
And that's that! Now that I've finally written a Wordpress plugin I expect to do it significantly more often. I expected there'd be a lot more BS involved in hooking extensions into the Wordpress system, and realistically anyone who can write even the simplest PHP functions can do this.

2 comments:

Oswald said...

Hello, I've found your blog by chance. I think this is Super.
I will regularly visit your blog. Please edit your
Blogjust as nice as you always do. It would be nice if you comment my blog. The link:
http://rentner-haben-keine-zeit.de
And if you have time the three Google Adsense advertisements with ID. They express a
small salary from U.S. cents and at least paysome of the bills.! My address can be found on my blog at Impressum. Thank you very much and have a nice day!

Schulte said...

Come on Sean, import this blog into WordPress...