Category: Web

Adding a PHP Program to WordPress

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.

The Problem

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.

The Approach

I’m a member of, 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:

  1. How do I add a custom page to WordPress?
  2. 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.

The Execution

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

The 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 of books.php:

$> head books.php
 * Template Name: books


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.

Installing the LAMP stack on Debian Wheezy

Otherwise known as a LAMP server, the LAMP software stack consists of GNU\Linux as the operating system, Apache as the Web server, MySql for a database, and PHP (or possibly Pearl or Python) as the programming language used to host a Web application.

I’m going to assume a minimal install (I’ll be using debian-7.6.0-i386-CD-1.iso)

First we’ll need to make sure we have all the necessary repos in our
/etc/apt/sources.list file. If you installed from a complete
installation image you probably won’t need to mess with this, but
cat out the sources list and make sure you have the following or
something simialr.

root@debian32-base:# cat /etc/apt/sources.list
deb stable main contrib non-free
deb wheezy-updates main contrib non-free
deb wheezy/updates main contrib non-free

Okay, now update apt and install our software packages.
You will need to set a password for MySql’s root user.

root@debian32-base:# apt-get update
---output omitted---
root@debian32-base:~# apt-get install apache2 mysql-client mysql-server php5 libapache2-mod-php5
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common libaio1 libapr1 libaprutil1 libaprutil1-dbd-sqlite3
  libaprutil1-ldap libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18 libonig2 libqdbm14
  mysql-client-5.5 mysql-common mysql-server-5.5 mysql-server-core-5.5 php5-cli php5-common ssl-cert
Suggested packages:
  apache2-doc apache2-suexec apache2-suexec-custom php-pear libipc-sharedcache-perl libterm-readkey-perl tinyca
The following NEW packages will be installed:
  apache2 apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common libaio1 libapache2-mod-php5 libapr1
  libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libdbd-mysql-perl libdbi-perl libhtml-template-perl
  libmysqlclient18 libonig2 libqdbm14 mysql-client mysql-client-5.5 mysql-common mysql-server mysql-server-5.5
  mysql-server-core-5.5 php5 php5-cli php5-common ssl-cert
0 upgraded, 27 newly installed, 0 to remove and 2 not upgraded.
Need to get 16.1 MB of archives.
After this operation, 115 MB of additional disk space will be used.
Do you want to continue [Y/n]? 
Get:1 wheezy/updates/main mysql-common all 5.5.38-0+wheezy1 [78.6 kB]
---output omited---

Right about now, you get hit with the ncurses screen

And then the installation will finish uninterrupted

---output omitted---
Creating config file /etc/php5/apache2/php.ini with new version
[ ok ] Restarting web server: apache2 ... waiting .
Setting up libhtml-template-perl (2.91-1) ...
Setting up mysql-client (5.5.38-0+wheezy1) ...
Setting up mysql-server (5.5.38-0+wheezy1) ...
Setting up php5 (5.4.4-14+deb7u12) ...
Setting up php5-cli (5.4.4-14+deb7u12) ...

Creating config file /etc/php5/cli/php.ini with new version
update-alternatives: using /usr/bin/php5 to provide /usr/bin/php (php) in auto mode
Setting up ssl-cert (1.0.32) ...

Now you should be able to navigate to the server with a Web Browser. Just type
the computer’s IP address into the address bar.

What we don’t know for sure at this point is if PHP is working. Let’s rename
the default index.html file to a PHP file (index.php)
(FYI: to rename in the terminal, we use the move mv command).
Then we can open it with a text editor and add some PHP code and see if it’s

root@debian32-base:~# mv /var/www/index.html /var/www/index.php
root@debian32-base:~# vim /var/www/index.php
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p
<p>The web server software is running but no content has been added, yet.</p>
<?php echo('Hello World'); ?>