Sep 26 2008

Question from a Reader: Hosting multiple WordPress blogs on one domain name - Part 2

Published by Jonathan Wise at 4:32 pm under Articles

Mike writes: So I’m a newbie when it comes to wordpress, but I think I can catch on fairly quickly. what I was hoping to find was something that would teach me to have one domain name, and have multiple blogs on that domain name, however, have a main front page, that would have that blog orginized into various preview posts.

This part of the question is significantly more difficult to deal with, since WordPress doesn’t offer this functionality (at least not in any way I’m familiar with!)

Mike pointed out a site called iBankCoin.com that had a front page concept similar to what he was trying to accomplish. The site, like many others out there, aggregated content from different sections (or different sub-sites) into one entry page. The layout is fixed, but the content is populated by whatever is most recent in the given section.
The ‘Peanut Gallery’ section could be a sub-site WordPress blog, where authors are Contributing users on that blog.

There are going to be two main technologies to pulling something like this off:

- RSS, or Really Simple Syndication, is an XML format that publishes the content of a website stripped of any styling, so that it can be simply consumed and read by other software — like a news reader or aggregator.

- PHP is the server-side programming language that WordPress uses to publish your blog content. Because WordPress uses it, we know that its installed on the server. There are other languages, such as ASP, that could accomplish the same thing, but there’s no guarantee that those will be available on your server.

In short — and I’m not going to be able to expand a whole lot without actually writing the code — we can use PHP and HTML to create our front page. Even basic HTML can do the layout we’re looking to borrow. We’ll use PHP, which runs on the server before the page is served up to the viewer, to populate the content of the page by pulling the RSS feed from each sub-site.

The RSS part of the puzzle is, by far, the easiest. WordPress has RSS publishing built right in, and if you have a WordPress blog up and running, so is your RSS feed! Just add /feed to the end of your WordPress URL.
So, using our previous examples, if we had a sub-site called www.jonscars.com/red-cars we could get the latest posts from it in RSS format by going to www.jonscars.com/red-cars/feed

The Entry Page, then, will be where the smarts of our system go. This page will contain both the HTML necessary to define the layout, and the PHP necessary to retrieve the RSS from each site, parse it, to extract only the information we want, then push it into the HTML as the page is loaded.

There are lots of ways we can do this, making it more intelligent (and complex) and there are lots of principles of good programming that would probably come in useful, but to keep things simple, here’s what’s going to happen at a minimum:

An HTML page exists on the server, with the special extension .php (for example, index.php works on most servers as a default page)

Before the HTML code, but in the same document, we’ll have a block of PHP code, called a method, that knows how to fetch RSS, single out the text we want, format it, and send it back:
<?php
// work your magic
?>
<html>
Your page goes here
</html>

Within the HTML code, in the places where we want the external content to appear, we’ll call that PHP method, telling it what RSS feed to retrieve:
<?php
// work your magic
?>
<html>
Your page goes here
<span>
<?php //request magic trick ?>
</span>
</html>

When the user pulls up the entry page of our site, the server will read through the .php file, find all the places where it needs to do some work, and execute our code, returning the results to our visitor as a completed HTML page, with all the PHP code gone:
<html>
Your page goes here
<span>
MAGIC!
</span>
</html>

Now I know this is hardly a complete solution. In fact, there are probably a number of ways to solve this problem, and this is merely a sketch of one way to deal with it. But one of the huge advantages to using PHP is that its a well loved little scripting language, and for anything you want to do, odds are, someone has already done it, or something similar, and published a how-to, or even better yet, a Library you can download, and use in your own code.

Case in point: by far the trickiest part of this project would be writing the code that gets and parses the RSS feed — its an easy to understand format, but you’d still have to write a lot of code to get at what you want. Fortunately, someone’s already done it. You can use Magpie RSS to handle all the difficult work for you, and you can reverse engineer this example of creating a more generic RSS Aggregator to fit your needs.

Picking up a new programming language can seem daunting at first — as far as I’m concerned, PHP has one of the most eccentric and ugly syntaxes I’ve ever seen — but Google makes for a great resource. Borrow, reverse engineer, and plaguerize any code you find published… that’s how every programmer I’ve ever met got their start!

Popularity: 9% [?]

Print This Post Print This Post   Trackback URI Comments RSS

Leave a Reply