0

i have a question and couldn't find a solution for it.

We have a Wordpress page where the client wants multiple error pages.

1 error page is in the usual normal 404.php for the custom theme which we created. Now i wanted to add multiple error pages with different styling and html structure.

example:

localhost/blog/{post-name} -> if this post name is not found load me the post-404-error.php

localhost/service/{service-name} -> if service was not found load the service-404-error.php

i couldn't think of a good solution how to get it done the right and save way.

i thought about changing the .htaccess like here described : 404 redirection to multiple error page htaccess

but if permalinks are saved the .htaccess is rewritten and my changed will be gone there also i didn't know how to put the correct error documents inside ( my rewriting htaccess knowledge is not the best) i thought there could be a coded way with php/js something?

i also thought about some complicating check if this url is called and then check if is_404() but i guess its to complicated

thanks in advance for any help and tips :)

Jaba
  • 99
  • 1
  • 8
  • 1
    I'm pretty certain you'd want to do this in the hook `template_include` or maybe `template_redirect`. You should have access to `is_404` at that point. – Chris Haas Oct 03 '22 at 13:37
  • thanks for this tip i start my research, for example when i try this in functions.php how can i archive to show the right template in the right url? – Jaba Oct 03 '22 at 13:57
  • see the last example here for a general idea: https://stackoverflow.com/a/67305267/231316 – Chris Haas Oct 03 '22 at 21:15
  • 1
    Cross-site post on the WordPress stack: https://wordpress.stackexchange.com/questions/410101/how-to-use-multiple-404-error-pages-in-wordpress – MrWhite Oct 04 '22 at 00:39

1 Answers1

0

Thanks MrWhite for your help on the Wordpress stack i didn't know that those sites were related. learned something new ^^

i managed to get it done here is my answer : https://wordpress.stackexchange.com/questions/410101/how-to-use-multiple-404-error-pages-in-wordpress/410133#410133

i can also copy it inside here, im checking in the 404.php in wordpress onlyt the url if the searched string is present and then show the template

$postUrl = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

get_header();



if (strpos($postUrl,'services') !== false) {
    get_template_part("error-pages/services-error-page");
} elseif(strpos($postUrl,'blog') !== false) {

    get_template_part("error-pages/blog-post-error");

}else{
    get_template_part("error-pages/general-404-error");
}




get_footer();
Jaba
  • 99
  • 1
  • 8