I’ve been looking all over the web for something like what I need and I haven’t find anything resemble not even similar, the thing is this:
I got a Page Template to a simple page post type, let’s say ‘Fruits’ and then in this template I am calling a Web Services that will bring back all fruits with a certain description, the url of this is www.mydomain.com/fruits/
I am not populating WordPress database with this information but taking it from an external service and I want to navigate the content with it details in WordPress templates design, so what I need is be able to click on ‘orange’ so I would be creating a link like this:
www.mydomain.com/fruits/orange/ —> This would be my permalink that is not registered
This non-existing permalink will redirect me immediately to the 404 Template page and it will be categorized in the wp_query object as ‘404’ = true, I want to avoid wordpress from processing it as a 404 and tweak it inners to send this request to a custom-template that will display the details of orange.
Please If you got any idea which hook can I use that doesn’t involve WP_Rewrite (which I tried all kind of combinations with no success) I will be forever grateful.
Thanks in advance.
As I hinted at in my comment, I would use the rewrite API to handle this, specifically by adding a rewrite endpoint. That way you don’t have to deal with working out what’s an actual 404, and what’s a request for one of your dynamically generated pages. It doesn’t give you exactly the URL structure you have in your question, but I think it’s a reasonable compromise in this case.
First we add the rewrite endpoint. In this example I’ve used
type
, so your URLs would be/fruit/type/orange/
:Now within your template, you can check if
type
has been set and you’ll know to fetch that keyword from your webservice:You also might want to look into the Transients API for caching the data locally, if you get a lot of hits and the extra http requests are slowing things down.
If you have no other option and have to use 404 redirection. You would use
template_redirect
action hook.