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.
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.
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.
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.
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 recommend using the wordpress cache module if your site is going to be popular!
Posted by: Pete White at June 18, 2007 8:11 AMGood tip from Pete White. Thanks!
Posted by: Fred at July 30, 2007 4:07 PMInclude?? 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 AMTrying 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 AMIs 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 PMThis 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?
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 PMHi,
The process you outlined is based on using an installation of Wordpress software on my site's hosting server, right? Are you aware of a way to do it if I want to use the blog I have on Wordpress.com (I'm not hosting it) without using an I-Frame on my site (which I'd like to avoid for SEO reasons)?
Thanks,
Jennie
Jennie,
This procedure does assume direct access to the WordPress PHP files, which you don't have through WordPress.com. If you have access to the templates through your WordPress.com blog (if you have one of their paid accounts, for example), you might be able to create page templates that would leave out all the headers and such, just outputting the content of the page. Then you could try using PHP's fopen() function to retrieve that page remotely, so there's no iframe involved. That's about the closest thing I can think of.
Posted by: Joe at July 12, 2008 4:35 PMThis is great, thank you! Can it be edited to include the titles and posting dates as well?
Posted by: sara at August 19, 2008 4:49 PMSara,
Sure. Where it says the_content() right now, you can add any other WordPress template function. In your case, you'll want to look at the_title() and the_date().
Posted by: Joe at August 19, 2008 9:47 PMI am attempting to get this working. I have the blog itself working in a page, but none of my graphics, nor my css is being called. It's probably something really dumb I'm missing. All I did was take a working page (which is designed with includes) and change the include to reflect the wordpress blog. That shouldn't cause everything to turn off should it? The blog works... but the page is not right.
Thanks,
Tina
Posted by: Tina at October 30, 2008 3:49 PMI should mention that I also just noticed that my links on the page are not pointing correctly. Darndest thing I ever did see! fbccolumbus.com/praises-from-fellowship.php/index.php Really screwy stuff. I'd welcome ideas.
Posted by: Tina at October 30, 2008 4:06 PMWell, I found the problem, or at least identified the cause. Somewhere in the call there is a / that is causing a screw up. I fixed it by hardcoding my graphics and links.
Posted by: Tina at October 30, 2008 4:20 PMTina,
I can't help you without seeing the code you're actually using. You can email me using the contact form on the site here and send me the code you're using. Maybe I can help you out and you won't have to talk to yourself here anymore. :)
Posted by: Joe at October 30, 2008 10:37 PMI read your explanation to Curtis about "include", but I still don't get it. What exactly am I supposed to write, and where? I don't know PHP at all.
And will I have to add that snippet of code manually (into the HTML file) to every new entry I want to create?
Posted by: Godheval at November 12, 2008 10:39 PMIdeally, what I'd like to do is edit my wordpress site - at http://godheval.wordpress.com, and have that content appear within my main site at http://www.godheval.net. Is that possible using this method?
Posted by: Godheval at November 12, 2008 11:03 PMHi Godheval,
This technique is, I think, entirely inappropriate if you don't know some PHP. This is not a method for installing or dealing with WordPress in anything resembling a normal way. I would recommend you look at WordPress the normal way, by building themes.
Posted by: Joe at November 13, 2008 12:36 AMI got someone who knows PHP to help me, so it's working now. The trouble I was having was in Step#4 of the instructions. It is not clear WHERE you are to post the include - that is, on line 1 of the new file (not in wordpress). It would also be helpful if you wrote out the code for the "include" for people like me who don't know PHP.
So now I'm excited that this works. But I have another question. Is there a way to incorporate WP's comments feature using this method?
Posted by: Godheval at November 13, 2008 10:40 AMhi!
Nice post! really works good and without problems. But is it also possible to display the content of certain wordpress pages on antother html site? I want to show the content of static wordpress sites on another html site selected by id as well.
Anyone knows how to do this?
Would be really nice!
Thanks
Here's the include code:
You also may need to display any errors in the file itself.
Try this code at the very beginning of your PHP file (literally on the line right after "<?php").
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting (E_ALL);
worked for me. thanks
Posted by: John at August 9, 2009 12:00 PMDo I need to change "DOCUMENT_ROOT"
I havent got this to work yet.
Posted by: PeeWee at December 24, 2009 8:37 PMWhat do you mean by paste in the current html content? Thanks
Posted by: Pippin at December 24, 2009 8:41 PMI am getting the following error:
Fatal error: Call to undefined method stdClass::set_prefix() in /home/xxxxx/public_html/wordpress/wp-settings.php on line 287
Sorry for my dumbness.
Posted by: Pippin Again at December 24, 2009 8:46 PMThank you. That helped me get content management of the ground without having to jump through the hoops of WordPress themes. Cheers!
Posted by: Kemble at March 21, 2010 6:11 PMHi,
is there also a way to get a worpress navi to be displayed that way? the point is that people (clients) do not want to integrate new links manually in the shop. when i create content with wordpress the links to the content will apaer. i need this to get working on a shop im creating.
Posted by: MeTaTuX at April 21, 2010 5:31 PMThank you for posting this!
Posted by: Simon at April 30, 2010 2:35 PM


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