Create A Helpful WordPress 404 Page
When your server can't find the requested page it will return a HTTP status code of 404. This tells the browser that the request has failed because the page you are asking for can not be found.
When your server returns a 404 this needs to be handled so that your not just displaying the default browser page.
Normally this will be done with htaccess by detecting a 404 error and redirecting the user to your own 404 page.
ErrorDocument 404 /404.html
If you use WordPress then this 404 redirecting is already built into the core. When WordPress can't find your page it will redirect the page to your WordPress 404 page.
To create a 404 page in WordPress all you have to do is create a new file in the root of your theme and name it 404.php.
Now that we have a page which will be displayed we need to customise this so that it's helpful to your visitors. It's good for the visitors to understand why they have got to this page, what this page means and what to do next.
Default WordPress 404
The default 404 page that comes with WordPress will display your theme header, sidebar, footer and a page title.
<?php get_header(); ?> <div id="content" class="narrowcolumn"> <h2 class="center">Error 404 - Not Found</h2> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
This tells your visitor what has gone wrong and that the page is not found but it doesn't give them anything extra to do, where are they suppose to go from here?
Add Useful Links
When the above 404 page is displayed the visitor is stuck with nothing to do but go back to your home page or click away from your site. The 404 page is a perfect place to add some useful links to help your visitors find what they are looking for.
- Add links to your posts
- Add links to your categories
- Add links to your author pages
- Add links to your archives
- You could even add a search box to the page.
Display List Of Page
To display a list of pages use the wp_list_pages() function.
wp_list_pages( 'title_li=' );
Display List Of Categories
To display a list of categories use the wp_list_categories() function.
wp_list_categories( 'sort_column=name&title_li=' );
Display List Of Authors
Use the wp_list_authors() function to display a list of all authors.
wp_list_authors( 'exclude_admin=0&optioncount=1' );
Display Your Monthly Archives
Display a list of archives by month by using the function wp_get_archives() with the parameter type=monthly.
wp_get_archives( 'type=monthly' );
Display Search Form
To display the WordPress search form use the function get_search_form(). This will search your theme folder for a file named searchform.php if it exists this function will return this file. If this file doesn't exist then it will return the default WordPress form.
My 404.php File
On my 404 file at Paulund I display the search form by using the get_search_form() function and then display a list of the archives by using the wp_get_archives( 'type=monthly' ); function.
What do you do with your 404 page?
Gain access to all tutorials
- Premium tutorials covering WordPress, Laravel and VueJS
- Download premium content
- Download premium WordPress plugins
- Only $5 a month
Join the weekly newsletter
Sign up for our newsletter. You'll be updated on new tutorials as they come available.