Oct 15 2007

iPhone Theme for WordPress

Published by Jonathan Wise under Code Snippets, Hacks

Here’s a hack for ContentRobot’s excellent iPhone Plugin + Theme additions for WordPress that allows the user to decide which version to view. When deploying this theme, I had originally chosen two different roll-out strategies — both modifications of what’s provided by the original code.

For jonandnic.com I made two versions of the website. This seemed like a worthwhile idea at the time, but now that I’ve further hacked the plug-in, I think the approach I used on this site is better.

In short, what I wanted to do was allow the user to choose which version of the site they want to view. The iPhone theme obviously has less features than a full widget/side-bar ready theme, and despite the small screen size, there are times that I want that functionality on my iPhone.
Initially I had a hack in that would ask the users with each page visit. This got annoying, so I improved my hack to include a cookie that saves the preference for an hour. This way they can decide on each visit which version they want to see.

This is a derivative work from the original plug-in, which is provided here, under the GPL.

Update: Here’s another note. Once the user’s made their selection, you can allow them to change their preference by adding the following code somewhere in your iPhone theme code. I added it to the Footer…

  • <p>View <a href="<?php bloginfo('siteurl'); ?>?iphone=no">Full Version</a> of this site</p>

2 responses so far

Oct 09 2007

WordPress Hacking: Multiple Blogs On One Set of Tables

Published by Jonathan Wise under Hacks

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 personal 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. To see it at work, check out:

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.

9 responses so far