Matt Boynes is a lead developer at Alley Interactive. He’s joining us this year at WordCamp Portland to explore the Rewrite API through examples. We caught up with Matt to ask him a few questions about his talk. Read on for more API goodness!
Why should I care about Rewrite Rules?
A good URL structure has a number of benefits. It’s essential for SEO and it can make a positive first impression. A great URL structure can do even more! It can lead to content discovery and serve as a breadcrumb trail. Most importantly, a great URL structure is easy to maintain until the end of time because it has structure and organization.
What’s the hairiest, most painful permalink structure you had to deal with?
By default, WordPress uses static “anchors” on rewrite rules for taxonomies and custom post types (e.g.
/event/wordcamp-portland/). That didn’t work for one of our clients, and they needed all their content types to share the same URL structure. Dealing with conflicting rewrite rules is hard enough as it is, but they also needed the URL hierarchy to be entirely dependent on the taxonomies in which an article has terms, and they needed everything to be overridable per-post. As challenging as all this was, WordPress is as flexible as a gymnast and we had our choices of how we were going to accomplish all of this. At first, we tried not using the Rewrite API to handle this, but in the end we found the Rewrite API to be over 100x more performant.
What’s the most underappreciated Rewrite API helper function and why?
It would definitely be
add_permastruct. I think a lot of people don’t completely understand when and how to use it, since it’s not as obvious as add_rewrite_rule. Once you get the hang of it, you’ll find that it’s an extremely powerful and DRY function, and one call to it can be the same as 10 calls to add_rewrite_rule. I could go on and on about it, but I already do that in my talk. By far the best feature is that you can use rewrite tags (tokens) like
%year% and WordPress will automatically fill in the regular expression.
What’s your preferred way of maintaining redirects?
In the same way WordPress uses regular expressions in rewrite rules, they’re oftentimes extremely useful in maintaining old redirects, so I use them whenever possible. Ultimately, there’s no one answer that solves for every situation, so it requires some creative problem-solving. Sometimes the redirects are simple and structured and the Safe Redirects Manager will give you everything you need. In other cases, when you have to account for tens of thousands of URLs with no logical structure, the solution may require some custom programming.
In making the decision as whether to maintain permalinks or not, what do you typically consider?
Permalinks should always work. The root of the word, perma-, leaves no mystery as to the intent, they’re meant to be permanent. Visitors bookmark, they tweet, they email links. Search engines crawl and store them. My grandmother writes them in a notebook. Visitors will always surprise you by coming to your site via a link that hasn’t existed for years, and the question I ask myself and my clients is: do you want those visitors to find that content or not? It’s rarely easy to do this, so you have to weigh the costs and benefits. More often than not, maintaining old permalinks is worth the effort.