I finally decided to do something about the abysmal visual quality of my site in recent times, but I definitely didn't want to compromise on complexity. Sometime in college, I started taking to heart the idea that it is rarely worth doing something, unless you plan to do it well. There is a modifier on that however: you need to define what *well* means for yourself. I had always held the opinion that my website was primarily for experimenting with new technologies or learning new things; having a visually interesting website was of distinct lesser importance. After I was in the job market this last I was informed — to my surprise — that my resume had been found via my own website more than through any other means. I cringed when I heard. Apparently, the visual effect of my site has become more important to me.
Back in the day I setup a WordPress and a Movable Type blog just to see how they worked, and over the past year I noticed enough respectable WordPress blogs that I decided to go with that option. Bringing back the point about needing my endeavors to be technically interesting, I decided to put a twist on my setup: I would attempt to integrate my full control of subdomains into the same WP installation. Little did I know quite how challenging that would prove to be.
The first obstacle I ran into was that WordPress 3.0 had just landed, and that meant that many plugins, and a few of the themes I tried weren't fully compatible. Not a big deal, I dove right in and started learning about the WP API and the types of things that broke from 2.8; A couple patches here and there and next thing you know the core pieces were working.
The basics resolved, I attempted to find a plugin that would do what I wished for subdomain management. No dice, though one plugin did stand out as being from the same family, and I was able to learn a bit from how it was structured.
Obvious next step? Start to a custom plugin to do what I wanted.
Key things I ended up learning much about:
- Use 302 redirects instead of 301 while debugging.
I lost some time attempting various (still flawed) solutions just to find that my webbrowsers had cached the 301 as the Permanent redirect that it is.
Crucially important, these are how your plugin gets the ability to override and inject data throughout the WP data flow. Basically there are a ton of keywords that WordPress broadcasts to at certain steps in it's life. This is effectively an observer pattern. In many cases WordPress will pass in its initialized array of data, and then ask all observers if they think it's correct. These observers include components across the system many of which might be plugins. These plugins have the option to remove or inject data based on their own logic, before the results are passed along to the next observer in the chain. Finally the potentially modified data is handed back to WordPress which then uses it.
- Permalinks and URL rewriting
I spent much too long learning how WordPress works in this case. Basically, there is exactly one initial entrance point to WP for everything of relevance to normal operation. The URL rewriting rules in the .htaccess just reformat the URL such that it passes data into index.php Further URL matching also happens at runtime in WP_Rewrite. I tried and I tried to figure out supplemental mod_rewrite rules to help create the subdomain effect I wished and had absolutely no luck. I tried to hook in to the additional rewrite rules and those were even more of a headache than mod_rewrite!
As usual, this principle seems to be a good guide. With every attempt frustrating and each subsequent attempt becoming more and more convoluted I eventually spent some serious time examining the flow of data in WordPress. I finally determined that the crucial datasource used to disambiguate which page to load is
$_SERVER['REQUEST_URI']. And with that realization came final success. Discarding the evolutionarily created mess and substituting one quick little method added in the plugin's startup, and Tahdah! Pretty permalink urls with subdomains in use, for every subdomain I care to map in my standard WP admin console.
After spending over a day frustrated on this problem, I'm elated to have solved it and I needed to write my first post. From my iPad no less!
PS. WordPress for iPad (the app) is extremely flaky, I typed my whole post in it and then it completely fails to actually submit the post (no error message) Additionally, until reboot, copy paste was broken in it so it looked like this post would be lost. Eventually, extracted, this post was published via the web admin console's raw HTML editor. Any recommendations for a better client? For the desktop it looks like I'll be using ScribeFire embedded in Safari unless someone has something better?
Mirrored from fbartho.com.