404 Error pages in WordPress – a few simple ways to customise your 404 Page

Share on Facebook0Tweet about this on Twitter0Share on Google+0

A quick guide to the 404 Error Page and WordPress. This post takes a quick look at the 404 Page and will show you how you can do some basic customizations to your 404 Page in WordPress.

The 404 Page – what is it?:
We have all seen them, the 404 page is the page that you see on a website when you request a page that no longer exists or cannot be found.

The 404 is an HTTP status code, and is probably one of the most recognizable errors on the internet and is understood by pretty much everyone regardless of your technical background or understanding.

They often look like this:
404 pages

If you aren’t sure how to view your 404 page then just type in a random url for a page that you know doesn’t exist on your site, maybe something like: www.yoursite.com/random111. That should trigger your 404 page.

Can you make 404 Pages better?:

In recent years, websites have gotten smarter about how they handle 404 errors – instead of showing users the bland, dull, and impersonal error page of the Web Server, why not use this opportunity to reduce bounce rates, or to make your user smile, or to drive them to more useful information, or to offer a solution etc…….basically anything is better than just a “Error 404: file not found” message.

This is a great (and short – only 4.08 minutes) TED talk from Renny Gleeson about the 404 Page, where he gives his own views on the opportunities of a good 404 page, and looks at some real world examples. Hopefully something here will inspire you to play around with your own 404 pages.

How can I customize my own 404 Error Page in WordPress?:

The good news is that most modern WordPress themes come with a 404 page (you will see a 404.php file in your website directory) that you can find easily in your ‘Editor’ in WordPress and edit.

  1. Go to ‘Dashboard
  2. Click on ‘Appearance’
  3. Click on ‘Editor’

From there you should be able to see your 404 Error template in the ‘Templates’ section on the right-hand side. Simply click on it to open it and edit it. You should be able to quickly spot the text that your current 404 page displays, and it is a simple task to change this – just type the new text that you want to appear and press ‘save’.

Here is what mine looks like below, with the text that I changed highlighted in the box.

404 page in wordpress

If your WordPress 404 page already has a search box and you are mostly happy with the layout and you just want to tweak the text a bit, then you can do that via this method.

Adding ‘Recent Posts’ or other content your 404 Page:

We have shown you how to change the text on your 404 Error Page above, but what if you want to add some extra features or provide links to other content on your site?

For our 404 Page we tweaked the text, and then added a few automated links to other posts and articles on our site – it won’t win any design awards, but at least it provides extra choices for any users landing on our 404 error page.

You can see it below:

New 404 Page

So how did we do it?:
We have added 2 pieces of content to our 404 page that updates itself automatically – ‘Popular Posts’ and ‘Recent Posts':

1: How to show Popular Posts:
Showing your popular posts (ordered by by page-view in our case) is a popular content module to show on a 404 page – after all, you may as well show your most popular content.

In order to do this you will need to install the WordPress Popular Posts plugin. This is a decent plugin that keeps track of your most popular content, and you can configure it to show popular content in the last 24 hours, 7 days, 30 days, or all time.

Once you have the plugin installed and activated, then just go to your 404.php page in the Editor add the following code in to the page, where you want it to appear:

<?php wpp_get_mostpopular("stats_views=0"); ?>

Note: We set the ‘stats-view’ to equal 0 as we didn’t want to show the number of page-views next to each entry (our numbers are still pretty low, so we didnt want to highlight that too much ;)). You have complete control over this, so if you want to show page view numbers then you can set ‘0’ to ‘1’.

2: How to show Recent Posts:
We also chose to show ‘recent posts’, and you can do this easily by simply adding the following code to your 404.php page:

<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 5, 'format' => 'custom', 'before' => '', 'after' => '<br />' ) ); ?>

Note: You can change the number of posts to show here by changing the ‘limit’ number. We have ours set to 5 (‘limit’ => 5,), but this could be any number that you want.

In addition to the Recent Posts and Popular Posts, we added a few header tags, line-breaks, and our final code looked like this – you can see the bits we added in bold text:


<?php
/**
* The template for displaying 404 pages (Not Found).
*
* @package Generate
*/

get_header(); ?>

<div id="primary" <?php generate_content_class(); ?>>
<main id="main" <?php generate_main_class(); ?> itemprop="mainContentOfPage" role="main">
<?php do_action('generate_before_main_content'); ?>
<div class="inside-article">
<?php do_action( 'generate_before_content'); ?>
<header class="entry-header">
<h1 class="entry-title" itemprop="headline"><?php echo apply_filters( 'generate_404_title', __( 'Oops! That page can&rsquo;t be found.', 'generate' ) ); ?></h1>
</header><!-- .entry-header -->
<?php do_action( 'generate_after_entry_header'); ?>
<div class="entry-content" itemprop="text">
<p>
<?php echo apply_filters( 'generate_404_text', __( 'Sorry! It looks like nothing was found at this location, we probably moved something or screwed up.... Maybe try one of the links below, or maybe a search?', 'generate' ) ); ?>
</p>
<?php get_search_form(); ?>

<br>
<br>
<h3>Popular Posts</h3>
<?php wpp_get_mostpopular("stats_views=0"); ?>

<h3>Recent Posts</h3>
<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 5, 'format' => 'custom', 'before' => '', 'after' => '<br />' ) ); ?>

<!-- .entry-content -->

<?php do_action( 'generate_after_content'); ?>
</div><!-- .inside-article -->
<?php do_action('generate_after_main_content'); ?>
</main><!-- #main -->
</div><!-- #primary -->

<?php
do_action('generate_sidebars');
get_footer();

There is lots more stuff that you can do with your 404 page. We intend to add some images to ours soon and generally make it look a bit nicer, and we will share that on the blog as soon as we do it.

Leave a Comment