November 13, 2006

Embedding WordPress

(aka WordPress as a content editor for the really, really lazy)

The best way to create websites that users can manage themselves is to use a proper content management system like Drupal (our current favorite). This is exactly the right thing to do if you have a reasonable amount of time, and especially if you’re building a site from scratch.

If the time or money is lean, you can cheat. WordPress is a fabulous piece of software designed to run weblogs, but is really easy to install and get running, and can be used as a content management system. Indeed, we’ve built a few sites on WordPress.

In one case recently, we wanted to retrofit an existing site with some content management capabilities. We’ve done this with Movable Type before, because MT generates static files, which are easily included in other pages using PHP includes or the like. The web host this client uses had PHP and MySQL capabilities, but not perl, so Movable Type was out and WordPress was in. The goal was to embed WordPress content in existing page templates without having to run the whole site in WordPress (not enough time available to go through that process — it’s not overly laborious, but it takes more time than we had for this project). Embedding WordPress content in other pages is quite easy; here’s how you can do it.

  1. Install WordPress, configure it however you want. In our case, we set up a category for each page to be edited (two of them here). Note the category ID numbers (probably 1, 2, and so on) for the categories you’re using — you’ll need them later.

  2. Write and publish posts in each of the categories, paste in the current HTML content. You can strip out paragraph and line break tags. Your content is now ready to be embedded elsewhere.

  3. Create a PHP file somewhere appropriate called wp_start.php. In it, paste code like this:

    <?php
        // turn off WordPress themes and include the WordPress core:
        define('WP_USE_THEMES', false);
        require($_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-blog-header.php');
    ?>
    

    Obviously, you might need to update the require line to point to the right wordpress location.

  4. In the page you want to edit, include your wp_start.php file, then insert this code where you want your content to appear:

    <?php
        // get the most recent post(s) in category 1
        // change category=1 as needed for whatever page you're embedding
        // (in this case, just we're getting just one post, but it could be more)
        $lastposts = get_posts('numberposts=1&category=1');
    
    
    
    // spit out the content of the post(s)
    foreach($lastposts as $post) {
        setup_postdata($post);
        the_content(); 
    }
    
    ?>

That’s it. Load the page in your browser, and you should see your content. You could, of course, do other things to get your content out — check out the WordPress documentation for more information on the template tags and functions that are available. There are probably a bunch of ways to do this, even using the same tools, so if you have something interesting to share, please leave a comment.

Thanks to Michael Wender for his post on how to embed WordPress in OSCommerce, which pointed the way to this procedure, in particular demonstrating how dead-easy it is to instantiate WordPress.

Posted by Joe

Comments

i tried to figure this out and it isn't working. I think it's because I'm screwing up the "In the page you want to edit, include your wp_start.php file" direction. what does this mean exactly? and is the wp_start.php file supposed to display anything like the index.php wordpress file does?

Posted by: jason at May 26, 2007 2:28 AM

I recommend using the wordpress cache module if your site is going to be popular!

Posted by: Pete White at June 18, 2007 8:11 AM

Good tip from Pete White. Thanks!

Posted by: Fred at July 30, 2007 4:07 PM

Include?? WHat does that mean?? Does it mean server side include??? If so when i do that it totally messes up my layout.. PLEASE HELP!

Posted by: Curtis at December 12, 2007 10:38 AM

Trying to figure this out. got an index to test with the include and the content loop.
I'm getting this error when I load the page.

Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\SWC embed\index.php on line 8

Posted by: Mike at December 17, 2007 12:18 AM

Is there a way to redirect the php calls for the comments pages to embed them in a similar webpage to the one withe the main wordpress content?

Posted by: CJ Luck at February 26, 2008 11:20 PM

This is working really well for me and I am very limited in my programming ability. Thank you.

It is allowing me to push up content from my blog to my "home" page and my "About" page.
On the home page, I only want the blog post title, so I commented out "the_content ():" and replaced it with "the_title ():"

On the about page I want both the title and content, but I would like to limit the number of words it pulls from the entry to 15. Is there a way to do this?

Posted by: Bob at March 15, 2008 10:38 AM

I've been rather negligent in keeping up with comments (email notification was turned off -- oops!). Apologies for that, everyone. Here are some comments, starting with the most recent.

@Bob: To limit the number of words, you'll need to be a bit more sophisticated in your PHP. The function that gives you the content of a post, the_content(), prints the content directly — it doesn't return the data for you to manipulate. The quick way to do this would be to use the_excerpt() instead, but that gives you 55 words. The solution is a little longer than I want to post here, but if this is still bugging you, you can email me and I'll help you out.

@CJ Luck: It sounds like you're getting into something more than this solution is meant to cover. While it is possible, I think, it's probably better to just make your site fully in WordPress.

@Mike: Without seeing the actual code you're using (I'm guessing there's more there than what I've posted), I can't say what's happening.

@Curtis: Here, include means literally the including code using PHP's facilities for doing so: include(), require(), and so on. It doesn't mean server-side includes that Apache and IIS provide (the kind that look like <--#include virtual="something.html"-->). This is not meant to be used by people who don't have some understanding of how PHP works, because it's kind of a hack, and you should know how bad an idea it is before you do it. :)

@jason: wp_start.php just tells your page to connect to WordPress and make its functionality available. It pulls no content by itself. I did it this way on the assumption that you might want to use this procedure on more than one page on your site. If that's not the case, you can just stick that code right in the one page and be done with it.

Posted by: Joe at March 31, 2008 2:36 PM
Post a comment









Remember personal info?