I recently switched my blog from my own programming to using WordPress. I have long recognized WordPress as good a software project and it has stood the test of time. Not only has the software been around since 2003, it has continued to improve. The improvements to WordPress have allow the software to run more and more projects (although it’s still used, in my opinion, too often where it doesn’t fit right).
For my needs I thought it would work well. I’m running a business while attending university and keeping up with certifications, so I didn’t want to spend the time to make it easier to post to my blog — which had been on my to do list — so I just installed WordPress. The installation took a matter of minuets and after a bit of time looking though themes I quickly had my site up and running with the CMS and looking decent with a freely available theme.
I wanted to add a table to my site that shows the books I’ve read. I wanted the book’s title to be displayed along with the author(s) and a picture of the book. I did not want to write out the HTML each time and the WordPress interface doesn’t make working with tables easy.
I’m a member of paperbackswap.com, and on their site they maintain a list of books that I’ve read. I can export this list as a CSV file. I thought it would be nice to place this CSV file in my site’s directory structure and have a PHP script parse the file and generate the desired table. This lead me to ask the following questions:
- How do I add a custom page to WordPress?
- How do I control that page with PHP?
Thanks to the user Adam Hopkinson on Stackoverflow, I had these questions answered and was ready to begin adding my custom PHP page to my WordPress site.
First, I created a directory for my custom pages in the WordPress directory structure named
my-pages. Then, I uploaded the CSV file from paperbackswap into this directory. I named the file
readBooks.csv. The fields of the CSV file were: title, ISBN10, and ISBN13. I really don’t need the ISBNs for my purposes but it didn’t hurt to leave them there (I may use them in the future, and I can always leave those fields blank when adding to the list), but I was going to need a field for the book’s cover image. I decided that would become the first field of the CSVs.
Next, I created the new page by following Adam’s instructions. To make things easier, I linked to this page (which is in the WordPress themes directory) into the
my-pages directory and named the page
books.php; my directory looked like so:
$> ls -hal lrwxrwxrwx 1 jason www-data 35 Oct 11 20:20 books.php -> ../wp-content/themes/vito/books.php -rwxrwxr-x 1 jason www-data 4.1K Oct 13 02:03 readBooks.csv
books.php page is a copy of my theme’s template, and contains its PHP code. I thought it would be too messy to insert my code directly into this template so I created a “controller” for it which contains the page specific PHP. Then I included the controller in the page so that I can call the function needed to print the books table. Here’s the
$> head books.php <?php /* * Template Name: books */ require_once(ABSPATH.'my-pages/booksController.php'); ?>
At this point, I started drafting the code that would parse the CSV file and print my table. I found that it was advantageous to create a Book class that would serve as a constructor. I would give the new Book object’s constructor an associative array containing the fields parsed from the CSV file. The Book objects would also provide a
__toString method which would return an HTML table row containing the given book’s data. The function that instantiates the Books would take care of generating the rest of the HTML table.
By now the
my-pages directory structure looked like this:
ls -F classes/ img/ booksController.php* books.php@ readBooks.csv*
I added the
classes directory to hold the definition of the Book class (and future classes, should I decide to create new pages) and the
img directory to contain the book covers. With all this in place, when I want to add a book to the Books page, I simply upload the cover image to the img directory and enter the book’s details in the
readBooks.csv file and voilà, the book would be added to the books table.