Have you ever wanted to make a static page use the same theme and features that your WordPress blog enjoys? It isn’t too difficult, and I recently did it for my Uptime/Downtime Calculator with just a few lines of code.
The calculator is actually a static page, but it has the same exact layout as my blog. I did this by loading in the WordPress header file, manually calling the template functions, and doing some quick hacks to my theme to make things easier.
Most of the work is done with these few lines of PHP code:
define('WP_USE_THEMES', false);
require(dirname(__FILE__) . '/../../../blog/wp-blog-header.php');
get_header();
The first one sets the USE_THEMES variable to false, so Wordpress doesn’t auto load the theme (which would make the 404 page appear, since WP is oblivious to the fact a static page was requested) and the second line loads in WordPress.
The next line is “get_header();”. Which, obviously, tells WordPress “hey, load in the header.php file for my theme!”
After that I manually create content for the page in a way that makes it looks like a regular post. This includes the H1 tag, meta data (written by… on… ) and the entry DIV. For my theme it looks a bit like this:
<h1>Title</h1>
By: Jeremy Steele
<div class="entry">Page content</div>
That isn’t too hard.
And finally I finish off the page by loading in the sidebar and footer by calling get_sidebar() and get_footer() respectively. Depending on your theme you may have to do get_sidebar immediately after the get_header function is called at the top of the page.
And that’s it. Well… actually… not really. Remember those theme hacks I mentioned before? This is where those come in. At the top of the page, before the header.php file is loaded via get_header(), I create two vars called $nusuni_special_title and $nusuni_special_stylesheet. The first one is the title for the page (it’ll end up blank otherwise, remember WordPress has no clue a static page was requested, it things it is just a nonexistent permalink), and the second is an extra stylesheet (if I need one).
For the uptime/downtime calculator I used this code:
$nusuni_special_title = "Uptime/Downtime Calculator";
$nusuni_special_stylesheet = "style.css";
In my header.php file for my theme I simply added some if statements to check if the vars existed. If they did exist the data was inserted.
To load in the extra stylesheet I used this code:
<?php if(isset($GLOBALS['nusuni_special_stylesheet'])) {
?><link rel="stylesheet" href="<?php echo $GLOBALS['nusuni_special_stylesheet']; ?>" type="text/css" /><?php
} ?>
It isn’t pretty, but it gets the job done. Basically it checks to see if I am using an extra stylesheet, then it loads it in if I am.
Then, for the Title I did this:
<?php if(isset($GLOBALS['nusuni_special_title'])) {
print("<title>" . $GLOBALS['nusuni_special_title'] ."</title>\n");
}
else {
?>
Print title tag info for normal blog
<?php
}
That checks to see if I am using a special title, and if I am it loads that instead of the titles I use for the blog.
While none of this is too difficult, it can be a pain to do if you haven’t any clue how WordPress works. If you need any help setting up a static PHP file to do this same thing, feel free to contact me (I won’t ask for anything in return, but a link would be nice
).
Please subscribe, or else I will cry. Do you really want to make a programmer cry?

September 2nd, 2007 at 6:17 am
[…] How To Load WordPress From A Static PHP Script by Jeremy Steele […]
September 2nd, 2007 at 5:34 pm
That was a good post. It’s something I’ve never actually tried before, though I can see how I might want to.
Thanks, bookmarked.
September 2nd, 2007 at 8:33 pm
Thanks
It’s one of those things that the wordpress docs tell you how to do, but not very well.