How I Enhanced My WordPress 404 Page To Be More Useful

August 20, 2012 / WordPress / By Kathy Alice


Frak! 404 Error Page
Flickr CC License - The Shifted Librarian
This week I wanted to enhance my blog’s 404 page to be more useful to my visitor. But I didn’t want to spend a lot of time and I didn’t necessarily want to use a plugin. Why not a plugin? I get worried about the overhead plugins add to my site and it’s not always easy to find one that does exactly what I want. Sometimes it is easier to just edit the code.

While Google Webmaster Tools does show I am getting 404 errors on my site, they are all due to the the lower quality guest author posts that I recently deleted. A spot check of the errors didn’t turn up any external links to these pages. So because a 404 is not a frequent occurrence for my site, it’s not a top priority (hence my upfront criteria that I didn’t want to spend a lot of time on this task). Nevertheless I wanted to make my 404 page more useful to my visitor and not a dead end.

Here’s how I added a search box and a list of categories to my 404 page. Now keep in mind that to follow along, I’m assuming you know how to copy and update files on your server and that you are comfortable editing php code.

What does your WordPress site 404 page look like today?

Before you get started check out what your 404 page looks like now by typing in something like yoursitename.com/foo (assuming of course you haven’t written a “foo” page). WordPress provides a 404 page with its default theme, and many themes have enhanced it by putting a search box on the page.

Updating your 404.php file – step by step

  • Since I have already created a child theme for customization, my first step was to copy the 404.php file from my parent theme (which had an enhanced version) to my child theme’s directory so any upgrades to my theme wouldn’t wipe out my changes. If your parent theme doesn’t have the file look for it in the /wp-content/themes/default/ directory.
  • Next I edited the actual message showing on the page to be something closer to something I would actually say instead of the vaguely British sounding apology.
  • My copy of the 404.php file already had the search box included, but if yours don’t, the code is

    <?php get_search_form( $echo ); ?>.

    The codex has more information on the get_search_form function. Of course you will want to add some HTML formatting to the code, if you are not sure how to do this, start out by enclosing the above code in <p> tags.

  • Next I wanted to list out my categories and categories descriptions so my visitor had another way to find my content. This takes a little more code, but as usual the WordPress Codex had a copy and paste example on the get_categories function documentation.

    There are several code examples, I used the “List Categories” one, which shows the category name as a link, the category description and the number of posts in the category. I’ve blogged earlier about changing my site to show my category descriptions to add unique content to my category page. So in my case I had all my category descriptions written, if you would like to do the same, you can update your categories by navigating to “Categories” in the right hand menu and adding a few sentences to the “description” field for each category.


    <?php
    $args=array(
    'orderby' => 'name',
    'order' => 'DESC'
    );
    $categories=get_categories($args);
    foreach($categories as $category) {
    echo 'name.' echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>'; }
    ?>

  • You could add code to exclude categories but that was too much work for me to consider. I made only one change, which is to have the query retrieve the category list in descending order 'order' => 'DESC' rather than 'order' => 'ASC'so that the categories would appear in reverse alphabetical order. As it turns out, this listed my more important categories first and was an easy enough change to do.

And then all I had to do was upload my file to the server and I was done!

For the lazy .. or the motivated

Of course there are WordPress plugins out there you can use if coding scares you. I haven’t tried any so I don’t have any recommendations.

For the really advanced, you could consider making your 404 page very smart and have it guess what the user was trying to find. This involves parsing the original URL the user typed in and matching it to a post that you already have. Yoast’s 404 post is a good place to start if you want to go down that route.

For me, this is more work than I’m interested in taking on. My philosophy is to not over-engineer a web site unless the user behavior is demanding a new feature or a fix. If I was seeing lots of users hitting my 404 page or I was a bigger blog, I would certainly consider doing more, but right now, enhancing my 404 page to offer a search box and both descriptions of my categories and links to them seems like enough for now.

Any good suggestions on WordPress plugins for custom 404 pages? Feel free to share!

Frak! Image courtesy of The Shifted Librian

About the Author Kathy Alice


Kathy Alice Brown is a SEO expert specializing in Technical SEO and Content. In her spare time she loves to get outside.

Leave a comment:


Your email address will not be published. Required fields are marked

  1. Read the ccontent carefully to see on your own what sort of author presents
    the facts with his fantastic personal views. They don’t anticipate thzt they can’ll
    be over-charged, too have an inadequate product which
    is shipped late, with a rude employee. Jeff Maehl held 9 paass for 133 yards in addition to 1
    2 indicate conversion.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Related Posts

css.php