Discover in this tutorial how to redirect to a custom page 404 errors (or “not found”) with a simple code snippet in WordPress.
Do you want to create a custom 404 page without having to create or modify the 404.php file?
It is true that you can achieve this with a plugin but there is another simpler way:
Creating the page you want to display when the URL doesn’t exist and make visitors see it with a redirection.
This way, you can easily create a custom page with the block editor (or even a page builder, although I recommend the block editor).
How to do this?
Using WordPress conditional tags and the wp_redirect
function.
Snippet to redirect 404 errors to a custom page
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 917 code snippets of this website, login or subscribe here.
It’s that simple.
In this case, you use the wp_redirect
function with the is_404
conditional tag so that when a page is not found, the website redirects to the custom page you created.
In this example, the redirection would be to the page with the slug “error404”, but you can modify this part of the code to match your custom page.
Conclusion
Now you know how to get all web visitors who get a 404 error to the custom page of your choice.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉