Building a 404 Page with WordPress Editor
As defined by Google, a 404 error is “an error message displayed by a browser indicating that an Internet address cannot be found”. You get a 404 message when you visit a link that does not actually exist. Many websites will display a custom message whenever someone visits a non-existent link because it improves the user’s experience and just looks more professional.
Recently, I was looking for a simple way to build a 404 page without having to mess with too much code. There’s a simple way to build a 404 page using the WordPress editor, just like you add content to any page or post. Here are the steps:
- Make sure you are using a child theme. If you aren’t, any edits made in the parent/main theme’s files will be overwritten when the theme is updates. A child theme prevents this from happening. You’ll need to copy the parent theme’s 404.php file to your child theme’s root.
- If you don’t want a specific 404 page and would like any 404 to simply redirect to an existing page, skip this step. Create your 404 page in the same way you’d create any other static page. You can include whatever HTML and shortcodes you’d like. For convenience, I’d suggest naming the page “404”. I’d also recommend changing the permalink to something like “/not-found” but you can certainly keep the “/404” permalink if you’d prefer.
- Finally, you need to include the wp_redirect function in your child theme’s 404.php (you should have copied it from your parent theme). This function lets you define a redirect path and a redirect type, such as a 301 permanent redirect. I just paste it at the very top of the 404.php and save. Your file will now redirect to the URL passed into the function. The code looks like this:
<?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>
Your new 404 page should be ready for action. Test it out by navigating to a non-existent URL on your website, such as yourdomain.com/thisisanonexistenturl.
Leave a Reply