Custom WordPress File Inclusion

First, Why this is a bad idea

WordPress already provides a rich templating API that allows for other plugins to hook in to your theme where needed. If a plugin needs to provide additional JavaScript or CSS, they tie in to wp_head. If they need to defer some JavaScript to the bottom of the page, they hook into wp_footer. If they add plugins, they use the built-in dynamic sidebar.

By completely bypassing the theme, you’re essentially preventing the user from doing anything with their site that you haven’t already built in to your display engine. You’re also throwing out about half of WordPress from the get-go.

An alternative

Instead, I suggest you work with the existing system. WordPress provides a lot of hooks, template tags, and conditional flags that let you detect which page you’re on and load different templates when needed.

There’s a really good discussion of the general template hierarchy in the Codex.

You can set up a different page template for each page, then when you write the page in WordPress you just select the right template.

So rather than include("$page.php"), you’d have three files in your theme:

  • page_home.php
  • page_mail.php
  • page_contact.php

Then, at the top of each of these files, you include the following header:

<?php
/**
 * Template Name: [YourPageTemplateName]
 * Describe what the template is for
 *
 * @package [YourThemeName]
 */

For example, from page_contact.php in my “Stacked” theme:

<?php
/**
 * Template Name: Contact
 * This file handles contact pages.
 *
 * @package StackedTheme
 */