Avtomatska rokometna lestvica (WordPress plugin)


Last weekend I developed for an acquintance a WordPress plugin which I’ve titled “Avtomatska rokometna lestvica” (translates to “Automatic handball league table” from Slovene). He’s a contributor to an official website of RK Trimo Trebnje, my local handball club and asked me if it’s possible to have a functionality which replaces the manual work of reading a website with statistics for a league table, selecting only a handful of columns and saving it to a blogpost. I had a lot of experiences with web scraping so I was immediately sure it can be done.

Without knowing where the scraped data will be displayed, I coded plugin’s option page using WordPress’s Settings API (and utilizing my library which simplifies creating option’s page). It’s just a textarea which accepts CSV-like data where each league table is represented in one line and one line contains a selection name and URL to its stats.

I have also coded a logic which actually scrapes the necessary data – table of team position, team name and team points and makes new table which is than saved into the database. For reading and constructing HTML tables I’ve used PHP’s DOMDocument. I’ve enjoyed working with this class.

Web-scraping is done periodically at midnight using wp-cron. They way wp-cron works is it launches when first user visits the site after the time it’s set to repeat.

Now comes the part where I learned how it’s going to be displayed. More than one league table needed to be shown (for selections like members, cadets, juniors,…), they were grouped in tabs. Inputs where they are edited were present on certain WordPress page using cusom fields. The problem for me was I had no idea where this fields are stored in the database. So the content from web-scraping process can be saved there.

I fixed this problem quite elegantly, I must say. I saved the content into wp_options table under a key {$package}_{$selection} and for displaying it I’ve coded a shortcode which queries wp_options for the key {$package}_{$selection} and returns HTML stored under that key – selection is specified as an argument to shortcode.