Integrate WordPress in existing site without copying anything

WordPress does something strange with the php include() statement. I don't exactly know why, but I get a lengthy error if I try to use it in a WordPress theme. The popular solution is to sit down and carefully copy and paste chunks from your website into a new wordpress theme. I'm here to fix that.

Every page on my website basically looks like this:

<?php include('header.php'); ?>
<h1>My Page title here</h1>
<p>My Content here</p>
<?php include('footer.php'); ?>

If this structure looks familiar, keep reading. We're good to go.

The code

Add this to your theme's header.php file:

<?php virtual("/header.php"); ?>

and this to your theme's footer.php file:

<?php virtual("/footer.php"); ?>

Easy enough. I'm sure the nomenclature could get confusing. If you prefer, you can rename your root header files. No file not found errors for files that exist like what happens when you try to use include().

Your new header file

Your header needs to be dynamic now. The bits in your header that load WordPress and alert browsers to your RSS feed's location are probably not relevant on other parts of your site. My solution to this was to set a variable in my theme's header.php file, before the virtual() statement. Header.php then checks if that variable is true before loading up WordPress and the feed location.

No rewriting, no maintenance

With this method, you maintain your same header and footer.php files like you would usually. Other bloggers advise you to recreate your entire site as a WordPress theme. That way, every link you add to your global navigation would have to be added to your WordPress theme's header as well. This way requires minimal changes to your existing header to work, and you only maintain one file.

Immune to upgrades

I had an old workaround for this problem before I discovered the virtual include. It involved setting constants in the index.php in the main wordpress directory, and referencing them in the theme. The trouble with editing the WordPress source is that it gets wiped if you upgrade.

Why not SSI?

The virtual command is basically equivalent to using server side includes. However, WordPress throws up the same error when you try using SSI.

Leave a Comment