DrQuincy Posted July 16 Share Posted July 16 How can I write a hook that happens when PW has dtermined the page is not found? I have some pages where the page name is a unique numerical ID (also stored in a hidden field) followed by a standard slug. E.g: /folder/4545-foo-bar /folder/89897-test-page If someone enters, to use an example above, enters site.com/folder/4545 PW will send a 404. However, what I want to do in the hook it search for a page that has the unique numerical ID. If it exists, redirect to that. If it doesn't, allow the default 404 behaviour to occur. I hope that makes sense. Thanks! Link to comment Share on other sites More sharing options...
Jan Romero Posted July 16 Share Posted July 16 You may want to use path hooks for this: https://processwire.com/blog/posts/pw-3.0.173/#introducing-url-path-hooks You can also hook into 404, but I don’t have a snippet handy right now. I’ll post one if no one beats me to it, but I think path hooks are probably the best way to do what you describe. Another option would be URL segments. You could define a regex to only allow a single integer URL segment. 3 Link to comment Share on other sites More sharing options...
DrQuincy Posted July 16 Author Share Posted July 16 Ah, great! You learn something great and new about PW every day! I can redirect, return a page or throw a 404 exception. Perfect! Link to comment Share on other sites More sharing options...
DrQuincy Posted July 16 Author Share Posted July 16 I'm don't know if I've missed something in your link but is it possible to add a path hook that runs when a page already exists in PW? This is esssential to what I want to accomplish. Link to comment Share on other sites More sharing options...
Jan Romero Posted July 16 Share Posted July 16 (edited) I think existing pages always have precedence over path hooks, so if a page called “/folder/4545” actually exists, no path hooks will run for such a request. But in your example, the page is actually called “/folder/4545-foo-bar”, so a path hook like this would trigger: $this->wire()->addHook('(/.*)/([0-9]+)/?', function($event) { $page = $event->pages->findOne([ 'parent' => $event->arguments(1), 'my_id_field' => $event->arguments(2) ]); if($page->viewable()) $this->wire()->session->redirect($page->url); }); This should find any path whose last segment is an integer, try to find the page with that integer among the children of the preceding path, and redirect to it. Normally I would test this before posting, but I’m not on a suitable machine… Edited July 16 by Jan Romero 1 Link to comment Share on other sites More sharing options...
DrQuincy Posted July 16 Author Share Posted July 16 That is super helpful, thanks. Quote But in your example, the page is actually called “/folder/4545-foo-bar”, so a path hook like this would trigger You are absolutely right there. Link to comment Share on other sites More sharing options...
adrian Posted July 17 Share Posted July 17 @DrQuincy - you might find this module useful: https://processwire.com/modules/process-redirect-ids/ 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now