Okay so my question is quite simple. I need to implement some custom routing rules for my plugin. Those routes would only take one argument (so nothing complicated) and would look like : http://www.example.org/myroute/myargument
And ideally, this would call a custom class, and display a custom template (that could directly access the class).
What’s the best approach for this ? Cheers
You need to do three important things:
index.php
.myroute
andmyargument
to WordPress’s query variables whitelist, so that WordPress doesn’t just ignore them when they appear in a query string.Firstly, I’m going to recommend that instead of
http://www.example.org/myroute/myargument
, you settle on some kind of special prefix or suffix to denote when the URI should be considered one of these special ‘routes’. For the the sake of this example, I’ve chosen the prefixapi
, so that it would behttp://www.example.org/api/myroute/myargument
. I choseapi
because when I did something RESTful, like what you seem to be working on, it was for an API.The Code
Quick Breakdown
It’s all fairly straight forward. The regex pattern is added to a list of all the rewrite rules in WordPress, and your custom pattern is at the top of the list. When the pattern is matched, WordPress will stop looking through the list of rewrite rules, and use the regex’s captured values in place of the references (
$matches[1]
and$matches[2]
) in the query string passed toindex.php
.Adding the query variables
myroute
andmyargument
to the whitelist just makes WordPress pay attention to them rather than discarding them.Alternative way of ‘namespacing’ your custom route
If you wanted to avoid using
/api/
as a prefix, you could use a query string variable/field instead. To do something like that, you would change the regex to something like(.*?)/(.+?)\?api=1
and then addapi
as an additional parameter to thearray_push()
call made inmy_insert_query_vars()
.That would change the custom route so that it triggers any time
api=1
is the first element of the query string, e.g. it would trigger forhttp://example.com/anytext/anytext?api=1
.Ignore the use of the term ‘namespacing’ – just used it for brevity.
If you don’t ‘namespace’ with either a prefix or a suffix, you will end up with colliding URI patterns. This is because WordPress will have no way to distinguish your custom pattern from one intended to be a post or page. How would WordPress know that
myroute
is not a taxonomy, term, or a parent page?Hope this helps.
To expand a bit on what eddiemoya did above:
Like the original poster of this question I wanted to create a custom rewrite, and also deliver a custom template for that rewrite page. The code from edditmoya got me started in the right direction, and I added an extra function to serve up my custom template when the page is accessed.
The custom template could be located anywhere, in my case it is stored in the plugin directory.
I also only wanted to check if the rewrite rules needed to be flushed during plugin activation, so I put that on a register_activation_hook
See below for the full example of what I did:
UPDATED simplified based on advice from milo