Archive for August, 2012

Wrapping a WordPress theme around a Static Page

Wednesday, August 8th, 2012

I have several websites that date back to the 1990s. These sites grew organically, sometimes without a theme at all and later with hand crafted shtml files and server side includes for menus, headings and footers. When I started converting things to WordPress, there was a tremendous amount of work and I never got back to some of the obscure pages. As a result, my main sites, JT30.com, harpamps.com, and cthreepo.com were never quite complete. Harpamps.com is only about 20% moved to WordPress because I had written a bunch of pages for that site over the years, and did not have the time to mess importing them to WordPress.

I have a dozen or so hand crafted themes on my sites and I wanted to bring them into a unified theme.

I decided to figure a way to wrap my WordPress theme around a static page. I managed to do it and it is not that hard. It is not as easy as installing a plugin, but it only takes me a few minutes now to get it working.

I wrote a few lines to go into the .htaccess file. These lines go outside and below the WordPress block in the file:

RewriteRule ^(.+)\.html$  /static/index.php [L,NC,QSA]
RewriteRule ^(.+)\.shtml$  /static/index.php [L,NC,QSA]
RewriteRule ^(.+)\.htm$  /static/index.php [L,NC,QSA]

These lines redirect any html, shtml, or htm (yes I’ve used all of them) to the php file in a directory I’ve named “static”.  The WordPress lines above it have already checked to see if a file exists, and if it does not, it has let WordPress do it’s permalink stuff to find the page. These three lines only execute if the html file really does exist.

The file index.php does a little checking on the requested url  to find the file and read it into a string variable. I then strip off everything except the body of the html file. I also strip out a few things that I might have hard coded like google ads or sidebar menus. (I used to hand craft every page. Most of the files have been altered at least once, but not all of them). In the future I may get rid of DIV containers with an id or class of head, header, side, sidebar, foot and footer. I have some recursive code that finds the DIV tag and the /DIV tag that matches. Right now, it is easier to leave most of it in. Sometimes the page is cluttered, but it’s not too bad.

I only got into trouble with a series of pages that I create on the fly with htaccess and PHP. I used to create ZIP files full of text and when a page is requested, read the zip on the fly and display the results. I had zip files that I used like databases. On of them had a million messages from an online mailing list and I made an efficient way to display them from the zipped messages. These have to be left alone and I can’t wrap WP around them, at least not yet.

I then include “wp-load.php” to get WordPress involved and then include a file that altered from the current theme that I called staticIndex.php. I have to hand code the exact location of the wp-load file and the path to the theme. I seem to remember there is a way to find the path to the theme directory, but it is just as easy to hard code it into the php file.

I copied either the index.php or single.php file to staticIndex.php and ripped the guts out of it to eliminate the loop.  The loop is the part that reads the list of posts to display.

Instead of the loop, I put a simple “echo $kpg_content;”. This is the variable where I had stored the body of the HTML that I read off the disk.

The WordPress theme does all the work loading up the header and the sidebar widgets. The page presents as normal HTML embedded in the right place in the theme.

I now have a consistent site. The static pages need an additional menu and I am working on that. Some of the directories do not have an index file and this can confuse things, but I am fixing that as I find them. I notice that there are lots of static pages that I never intended for the public, and I need to delete these as I find them.

That’s all there is to it. I charge $100 an hour when I find time to consult, and I did this in about an hour, most of that time was spent in testing and tweaking some of the badly behaved pages.

Now that I have this procedure working, the next logical step is to make a plugin that installs the htaccess code and automatically configures the code. I am not sure how I can do this. I can see it getting complicated, though. You may want to have an htaccess file in only some of the directories and leave others alone. I also don’t know how to foil the theme from using the loop and displaying the static page instead. I have some code that does something like that, but some themes do some strange things with the loop.

I have a site, harptab.com which is 90% dynamically generated pages using a variety of templates. I am tempted to install WordPress on the site and use only the welcome page. All the rest of the pages would be the old pages, except that they use the WordPress theme as a wrapper. In this way I can control the sidebars, menus, ads, etc from WordPress an never have to alter the individual files again. This is going to take some work, though.