Wise on Tech Hacks, scripts and ideas for the refined geek.

26Sep/080

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

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.jwcars.com/red-cars we could get the latest posts from it in RSS format by going to www.jwscars.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!

Tagged as: , , , No Comments
17Sep/081

Question from a Reader: Hosting multiple WordPress blogs on one domain name – Part 1

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.

So this is a relatively easy job, but it should be noted that there are probably numerous ways to accomplish what Mike is after. His problem really is in two parts, so I'll offer my solution to one, and later, some ideas at how to solve the other...

The good news is, WordPress is made to do this. In fact, I'd wager there's already some documentation on their site on how to do it. Its worth discussing though, just to get ideas flowing.
Probably the first thing you'll want to do is come up with a naming strategy for your sub-sites. Do you want to use www.mysite.com/subsite1 or do you want subsite1.mysite.com? Actually it doesn't matter from an installation perspective, but its certainly a useability concern, and one approach will dictate some additional work with your hosting provider.

Here's a screen shot of the top level web directory of a website. You can see each of my subsites, as well as some supporting folders for the entry level site...

I chose a naming convention that reverses the domain names, simply because the sort order makes it easier for me to find things. Note that in the subdomain approach (subsite1.mysite.com) the folder names don't really matter -- I'm going to hide them from users. If I chose a subfolder approach (mysite.com/subsite1) the folder names would be important (at least in a simple setup).

In Mike's case, the entry site (or root site, or main site) will not contain a WordPress installation. This is because what he wants to do can't be done by WordPress (see part 2.) In my case, the root site doesn't contain a WordPress install either, but through some magic I'll discuss later, the top domain redirects to a WordPress-driven sub-site

Note, for interests sake, that I can also host sites with unrelated domain name is the same manor. For example, my parent's site, www.spwise.com, bounces off my server -- it currently just redirects to another site, but I host the domain name.

Ok, now if all of that is as clear as mud, here's where it gets easy. Download the latest WordPress zip archive, unzip it, and upload it as many times as you want to your server -- one for each subsite you want to have. Just make sure you rename the "WordPress" folder each time, to avoid over-writing it.

So if I wanted to make the site www.jwcars.com and have the following subsites www.jwcars.com/blue-cars, www.jwcars.com/red-cars, www.jwcars.com/green-cars, I'd upload the WordPress folder 3 times, and rename the folders to blue-cars, red-cars and green-cars. Your web root folder would look something like this...


Each folder is a separate and complete WordPress installation

Now use your browser to go to the WordPress install page in each of those folders, and set-up the sites. At this point, each of the sites should have its own set-of tables in your WordPress database. You can share registration/log-in information between the sites, by doing a little hacking in the WordPress code -- that's outside of the scope of this write-up, but you'll find some tips on sharing config tables between multiple blogs here.

If you want to use the subdomain approach, you'll need a decent web hosting provider that lets you have access to some more advanced stuff. I'd imagine all the good ones let you do this, but I use WestHost and have been very happy with how much they let me configure on my own. Here's how I configure a subdomain on my server. You'll need to do something similar, but the exact steps will vary by service provider...


Here I point the subdomain testblog to the server path /var/www/html/com.server.testblog

You now have n number of websites running on one server. The only problem is, there's nothing running on your entry site. If people go to www.jwcars.com/blue-cars everything looks great. But if they go to www.jwcars.com they'll get a 404 error, or worse.
In part 2, we'll discuss some ideas for how the entry site might work. Its considerably more complicated, and requires a lot more custom coding, so I won't be able to provide the actual solution, but WordPress offers enough functionality to get us off on the right foot...

Tagged as: , 1 Comment
9Oct/0720

WordPress Hacking: Multiple Blogs On One Set of Tables

Note: This post was written for WordPress 2.1. Please see the comments for discussion on how to apply to newer versions!

I recently rolled out a new theme for my website. Normally when I do this, I do it against the live site, so there's about an hour where the site is either broken, or looks really weird while I'm working on it. This time I wanted to be a little smarter.

The goal was to build another blog -- a "test" blog where I can try out new themes and other experiments -- that pointed to the same data as the main site. This way I could get a realistic picture of what the site would actually look like before I publish an updated theme.

The more I thought about that, the more useful the concept seemed. What if I wanted an iPhone-friendly version of the site? I wouldn't want to write the same post in two places. Now eXpression could do this very easily -- since every "page" was actually a query, you could just specify a different theme file (XSL) in the query. WordPress, not so much.

What WordPress can do is allow you to specify a table prefix for each installation on a given server. That means you can have 3 blogs, each with their own set of tables. When you build your wp-config file, you tell it what prefix to use. Since this is the case, it should be fairly easy to install another instance of WordPress and point it back to the original blog tables, right? Here's how you do it...

Step 1 - Install a new instance of WordPress

  • Make sure it has a unique folder name so it doesn't over-write your original blog
  • Run the install pages, and give the new blog a unique table prefix
    • IE: If your original blog got the default prefix: wp_ then give the new blog newwp_
  • Get the blog up and running with an admin account, but don't bother posting anything

Step 2 - Point the new blog to the original blog's tables

  • Modify wp-settings.php as per the instructions found here
    • Modify all the table pointers EXCEPT for Options
  • This means that only the Options (including Theme and Plugins) will be specific to your new site. Everything else (including Posts and Comments) will come from the original site
  • So anywhere you see: $wpdb->users = $wpdb->prefix .
    Change it to: $wpdb->users = 'wp_' . 'users';

    • where 'wp_' is the table prefix for your original blog

Step 3 - Grant the admin user from your original blog the same permissions on your new blog, by inserting two records into your wp_usermeta table:

  • Where wp_ is the table prefix of your original blog and newwp_ is the table prefix of your new blog...
    • INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES (1, 'newwp_user_level, 10')
    • INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES (1, 'newwp_capabilities', 'a:1:{s:13:"administrator";b:1;}')

That's it! You now have two uniquely styled blogs with the same data in them! The only thing that sucks is if you have Sidebar widgets configured, you'll need to re-configure them in your new blog as well and manually keep them in sync. That's the case for all options, and that's why this is useful.

A couple other notes:

If you want, you can delete the superfluous "new blog" tables -- just make sure you leave the Options table intact.

I have no idea how this will work during a WordPress upgrade. My current plan is to run the upgrade on the original, or base, blog, then manually change the Options table structure, if necessary, for the secondary, or derived, blogs.

Tagged as: 20 Comments