Wordpress & Web Development

How to Set Up a Proper 404 Error Page on Your WordPress Blog

James Bruce 02-03-2012

The humble 404 has been with us since the tubes that make up the internet were first plumbed in. Websites can actually return a whole smorgasboard of different error codes, but 404 is the most common and generally referred to as “not found“.


Sometimes your browser will handle these and return a generic error page, but because WordPress infers content from the URL dynamically (as opposed to there being an actual file on the server), it handles errors internally too.

Unfortunately, the default message is pretty much as unhelpful as you can get, so today I’m going to show you how to tweak your WordPress 404 error page to be a whole lot more useful.

DIY or Friendly 404 Messages

You may have noticed we have our own custom error page at MakeUseOf which is somewhat more welcoming than the default message. To make your own custom error page, simply create a template called 404.php in your theme folder – if you don’t already have one that is. Duplicate the single page template if you like, and cut out the main content area. You can then either hand code an image like we have, or write your own simple message. Then you start adding some advanced functionality.
404 error

Email You About the Error

Usually when someone hits a 404 error page, it’s because they’ve followed a bad link somewhere rather than typing a silly URL directly into the address bar (though, never underestimate their ability to do that too). In those cases, it would be helpful for you as the admin of the site to know about the error, and either correct your own bad link or to ask the site linking to you to correct it. Paste this code snippet in full to the very top of your shiny new 404.php page:

<?php $message = "";
$message .= "User came from: ".$_SERVER['HTTP_REFERER']."\r\n";
$message.="Page URL they tried to access was: ".$_SERVER['REQUEST_URI']."\r\n";
$admin_email = get_option('admin_email');
@wp_mail($admin_email,"404 error",$message);

The above code will send an email to the registered admin email address describing the URL they came from, if available, and the URL they tried to get to. On the user side, it does nothing – it only emails you, and doesn’t output anything onto the error page itself.


Add a search form

The user obviously came to your site looking for something, so rather than leave them frustrated, let them search for what it was they wanted instead. To do this, find a suitable place on your template, perhaps add a little note about “Why not try searching instead?”, and paste in this single line of code:

<?php include(TEMPLATEPATH . "/searchform.php"); ?>

Plugins to Enhance the Error Page

If custom coding a 404 page isn’t really your thing, then no worries because there’s a few plugins that can give you a better 404 experience too. Let’s take a look:

404 Redirected

This plugin monitors your site for 404 errors and records them so you can be alerted from the admin panel. Moreover, if you find some external site has linked to you with the wrong link, you can redirect it without having to contact them – just tell the plugin where that broken link should be going, and visitors will be sent right along with a search engine friendly 301 automated redirection.

404 error


Smart 404

Even better than trying to fix the 404 errors as they popup would be to never show them in the first place. Smart 404 does exactly that – it looks at the URL, extracts keywords from it, and tries to figure out exactly what the user was trying to go to. There’s no screenshot to show you, because it doesn’t need any configuring and just works out of the box. The downside is of course that if the URL contains nonsense keywords, it’s unlikely to find any matches – so spelling mistakes are also out.

Anyway, I hope you decide to make a proper WordPress 404 page, either DIY style or with a plugin. Do you know of any other clever 404 related plugins you’d like to share, if so then let us know in the comments! We also got a rapidly growing archive of fairly advanced WordPress tutorials, as well my own Getting Started with WordPress Set Up Your Blog With WordPress: The Ultimate Guide Want to start your own blog but don't know how? Look to WordPress, the most powerful blogging platform available today. Read More free guide – which covers everything from essential first steps and plugins all the way to monetizing and SEO; and be sure to check out the Best of WordPress Plugins The Best WordPress Plugins Read More page.

Related topics: Web Design, Wordpress Plugins.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

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

  1. karthik chandrappa
    July 2, 2012 at 3:16 am

    Will multiple 404 related plugins conflict ech other?

    • James Bruce
      July 2, 2012 at 8:02 am

      Depends on the plugin in question. If you have one to log a 404, and one to automatically redirect, i dont suppose they would conflict. Obviously ,two plugins designed to redirect a 404 wont work.

  2. Edz
    April 15, 2012 at 5:38 pm

    Hi, does this will help to prevent 404 error in Google Webmaster? I hope you'll reply on my comment

    • James Bruce
      April 16, 2012 at 8:24 am

      No, this won't prevent 404 errors Edz. To do that, you would need to log the errors and fix them in the first place.

  3. Elga
    March 16, 2012 at 9:05 pm

    Some of the newest WordPress themes have this search function in 404 pages already set up. Mine has it and I'm glad. I need to work out a little bit on the design. 404 pages have to be nice as the rest of my website.
    Very useful info here. Thanks for putting it down for us!

  4. Money Saver
    March 2, 2012 at 8:04 pm

    Great article! I am working on my own 404 page, our blog is fairly new but I have made a ton of adjustments (built on wordpres) and am learning a lot from sites like yours, thanks for that!

    Mike from Moneysaver Blog http://www.moneysavermag.com/blog