WordPress: Sharing database content between primary and sub-domains

After I created my mobile WordPress theme using the the Mobile Theme Generator that I created on MobilePebbles.com, I had to figure out the best way to share the database between my primary domain and the mobile sub-domain (m.grasshopperpebbles.com). There are a couple of strategies for switching between a desktop view and a mobile view. I decided that I didn’t want to do an automatic switch to the mobile sub-domain by using a mobile device sniffing method. I think that it is a better strategy to let the user decide whether they want to switch to a mobile view. So I set up a sub-domain and created a link at the top of my site that points to my mobile view.

I followed these steps to share database content.

  1. Before creating the sub-domain, Update WordPress and all of the plugins on your primary site.
  2. Install WordPress on the sub-domain (make sure that is the same version that is on your primary site).
  3. Delete the wp-content folder from your sub-domain.
  4. Copy the wp-content folder from your primary domain to your sub-domain.
  5. Copy/Paste the database configuration from the wp-config file of your primary to the wp-config file of your sub-domain.
  6. Open the functions.php file of your sub-domain (wp-content/themes/) and define WP_HOME and WP_SITEURL as your sub-domain (Note: The Mobile Theme Generator does this for you).
  7. define('WP_HOME', 'http://m.grasshopperpebbles.com');
    define('WP_SITEURL', 'http://m.grasshopperpebbles.com');
  8. If you plan to have a different WordPress theme on your sub-domain (like a MobilePebbles theme), be sure to change the folder name to match theme folder on your primary domain. For instance, if your primary domain is using a theme named grasshopper, then change the folder name of the theme on your sub-domain to grasshopper. You have to make this change because the database will be looking for a folder named grasshopper.
  9. That’s it. Now you will be able to share WordPress database content between your primary and sub domain. The one caveat: By adding the WP_HOME and WP_SITEURL definitions, all of your internal links will map appropriately to your sub-domain except those that are created within a post.


    http://grasshopperpebbles.com/archives will map to http://m.grasshopperpebbles.com/archives


    If you happen to link to another post within your post, the visitor will be directed to your primary domain. In the next few weeks, I plan to create a plugin that will handle those situations.


Be Sociable, Share!

Checkout My New Site - T-shirts For Geeks