The WordPress Rewrite API is a solid API assuming that you’re comfortable with regular expressions.
And, honestly, even if you’re not, tools like PHP Live RegEx make it really easy to test your code before dropping it into the
add_rewrite_rule API call.
However, there are times where you may find yourself in a situation in which you need to work with rewrite rules and page templates. And when that’s the case, it’s not always as straightforward as making a simple call with a single regular expression.
The reason? If you have multiple pages using the same template, then you’re not always going to have the page located in the same index in the query that runs on each page.
To that end, we to have to handle it in a slightly different way than, say, simply accessing a numerical index of information in the global query.
Rewrite Rules and Page Templates
First, let’s assume that we have two templates (that are in no particular order):
And then let’s say we want to set up a custom URL for pages each of which use one of these templates.
This would break down into the following steps:
- Iterate through the posts in the query,
- Store the post using the template in its respective array,
- Set up the rewrite rules for each post having a given query.
For this example, I’ll be keeping the rewrite rules pretty simple. After all, regular expressions can be hard enough to follow so trying to follow those while dealing with the Query object, multiple arrays, and handling the rules can be a bit much.
With that said, here’s sample code that’s broken down based on the above steps. At the end post the post, I’ll share a snippet that throws everything together in a few separate functions so you can see how all of this fits together.
With that said, here’s how to handle all of the above.
1. Adding Rewrite Rules
First, we need to read some information from the global WordPress query object to find all pages that have templates applied to them.
To do this, we can read the information from the global query object into a local variable. This makes it easy to duck out of the rest of the code if there’s nothing with which to work.
2. Store Relevant Information in an Array
Assuming that there are pages having templates, then we’ll proceed with storing each of the pages into arrays so we can easily manage their respective rewrite rules.
Note that in the above, I reference a global array. Assume this is simply initialized as an empty array in the constructor of the class.
3. Setup the Rewrite Rules
Now that we have two arrays – one for each template and the pages that contain it – we can iterate through them and assign rewrite rules for each of those pages.
For example, let’s say that for one template, say alpha, we want to setup rewrite rules for the US and states.
Here’s code for
And here’s code for
Of course, the regular expressions used in the code above are simply for demonstration purposes. Yours will be for whatever is needed for your project.
4. All Together Now
It’s important to break all of this functionality into its own methods so that it can all be easily encapsulated and tested, if necessary.
To that end, here’s what the full code will look like:
Remember, though; this is just based on the demo code shared in this post. When you’re working on your project, the regular expressions, templates, and so on will vary.