How to Make HTML Pages Work Like WordPress

Wordpress logoDo you have a bunch of standard html pages outside your WordPress blog heirarchy? I’m not taking about WordPress’ static “Pages” with a capital P, but those pages, perhaps in the root beyond the CMS facility of WordPress. Well, there is a relatively simple way to convert a standard html page into a kind of WordPress page, so that your header, sidebar, and footer all render as if the page were a standard WordPress page.

1. First, make a back up of any pages you plan to change, then add this line to the head section of each page you want to re-theme within the WordPress system on your site.

<?php define(‘WP_USE_THEMES’, false); require(‘./your-wordpress-folder/wp-blog-header.php’); ?>

That calls in the necessary code from the WordPress folder (obviously change “your-wordpress-folder” to the actual directory name).

2. Next, decide which components you’re going to need for the page. You’ll probably want the header from your theme so add

<?php get_header(); ?>

Put that near the top after the body tag.

3. You’ve probably got a footer in your theme too, so add this to the bottom of the page before the close body tag

<?php get_footer(); ?>

4. Then comes a tricky bit. Where to put the sidebar? It depends. Try it just before the footer tag you added in the previous step first. We can come back later and move it if it doesn’t work properly.

<?php get_sidebar(); ?>

5. Now, smart readers will have noticed you’ve probably been adding a load of php tags to pages with an htm or an html and that opening these in your browser is just going to display the code without running it. So, there’s one final critical step you must take before things will work as planned. Open up your .htaccess file (you know where that is I take it, root folder, no filename just the extension htaccess (don’t forget the dot). Make a backup copy. Then add the following line:

AddHandler application/x-httpd-php .php .html .htm

and save the .htaccess file. This will then allow the browser to parse the php in pages with the htm and html extensions as well as the php extension.

Couple of things to watch out for: if you cut and paste the above code, ensure the quotes are single straight quote marks and the angle brackets are proper code not greater and less than signs.

Also, make sure you double check the resulting meta tags for these pages, it might result in losing RSS autodiscovery or changes in your carefully honed title tags. Also, you generally will not be able to enable comments on these pages as you might do with a static Page within WordPress proper. If anyone knows how to workaround the comments disabled default for this kind of page, please leave a comment. I’d love to enable comments across the whole of the site on which I make extensive use of this format.

Author: David Bradley

Freelance science journalist, author of Deceived Wisdom. Photographer and musician.