picture home | pixelblog | qt_tools

omino code blog

We need code. Lots of code.
David Van Brink // Thu 2008.11.6 10:44 // {wordpress}

WordPress Pages, Anywhere

Pardon this post which is about technical trivia unrelated to API’s and Weird Blinking Lights. But I needed to jot it down somewhere. It’s a note about including contents out of WordPress in parts of a site which aren’t under the WordPress installation.

Now that WordPress includes revision management (as of 2.6) I intend to use it for the whole site, but I didn’t want to rejigger the install directories. Recipe below the cut.

There’s a few recipes floating around describing how to use bits of content out of your WordPress database in any web page. This post on corvidworks seems to be a popular one…

But I wanted something almost lighter-weight — a way to simply “insert” a regular WordPress page or post at an arbitrary location of my site.

Two ways to do this would be with .htaccess rewrite rules, and by making simple pages which are solely and completely an iFrame. Those methods are less direct, but impose fewer dependencies on the particulars of the WordPress API.

Anyway, here’s a sample file that inserts a WordPress page using the WordPress API.

This file demonstrates displaying an item out of the WordPress database
from an arbitrary web page, not necessarily anywhere near the
installation of WordPress.

By david van brink poly@omino.com 2008
Use any way you like.

The gist is straightforward:
 1. Load up the WordPress API and Library, but with WP_USE_THEMES=false,
    so nothing actually gets rendered.
 2. Use query_posts() to choose the content. This affects The Loop
    whenever it gets invoked.
 3. Attempt to use a particular page template from our Theme... but
    if it's not there (perhaps the current theme doesn't have it) fall
    back to the safe-and sane index.php.

You need to find wb-blog-header.php relative to this page, in the file system.

Some handy constants that WP provides are TEMPLATEPATH, to the
current theme, and ABSPATH, which is the root of the WordPress installation.

Some themes won't work with this. For example, 2Exquisite fails because it
assumes the current directory is the WordPress install.
Line 3 of index.php reads:
It can be fixed by changing it to:
        require(ABSPATH . '/wp-blog-header.php');

Ok, let's go.

// Load up the wordpress API and current theme, but don't render.
define('WP_USE_THEMES', false);

// Choose the page or post or posts to show

// And show it, or them, preferring a particular template if available.
if(file_exists(TEMPLATEPATH . "/page1.php"))
    require(TEMPLATEPATH . "/page1.php");
    require(TEMPLATEPATH . "/index.php"); // everyone's got this
oh, i dont know. what do you think?

(c) 2003-2011 omino.com / contact poly@omino.com