How To Create a Custom Error 404 Page For WordPress

This article will guide you how to create an error 404 page for your WordPress powered site or blog. This guide will work for self-hosted blogs, not for free ones on WordPress.com like (xyz.wordpress.com) ! An error 404 page is when you try to access a page that does not exist.

What is the error 404?

The error 404 is a message shows up if you click on a link that doesn’t work (anymore) or if you typed wrong Url. This is default in WordPress but some themes don’t come with a 404.php file.

Basic Error 404 Template

If you do not have default 404.php page, you can create it. Create a 404.php file with the below code..

<?php get_header(); ?>

<?php get_sidebars(‘left’); ?>

<h2>Error 404 – Page Not Found.</h2>

<?php get_sidebars(‘right’); ?>

<?php get_footer(); ?>

The above code would give a simple “Error 404 – Page Not Found.” as ouptput, which is mentioned within in h2 tag. You need to modify header, sidebar & footer as per your theme structure.

404 page with Search Form

Want to add search form to help users stick around instead of leaving. By this way  visitors have the option of searching your site, even if lands on your 404 page. If you want to add search form for your 404 page, add the below code..

<?php get_header(); ?>

<?php get_sidebars(‘left’); ?>

<h2>Error 404 – Page Not Found.</h2>

<p>Search:</p>

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

<?php get_sidebars(‘right’); ?>

<?php get_footer(); ?>

404 page redirect to home page

If you want to just redirect 404 page to home page then add the below code in 404.php

<?php

header( ‘HTTP/1.1 301 Moved Permanently’ );

header( ‘Location: ‘.home_url( ‘/’ ) );

?>

Make it more dynamic

If you want to create a more dynamic error 404 page you can use this way so that the visitor only sees the brief error and, then gets redirected to your home page. This page can be made to be somewhat SEO friendly. For this you need to edit the header.php file of your template. Within the meta tags at the top of your header.php file add the below code ..

header.php

<?php

if (is_404()) {

*$redirectHome = get_option(‘home’); ?>

<?php echo $redirectHome; ?>

After added it into header.php file, you need to edit your 404.php file to look like this:

404.php

<?php get_header(); ?>

<?php get_sidebars(‘left’); ?>

<h1>Error 404 – File Not Found.</h1>

<h3>Please <a href=”<?php bloginfo(‘home’ Or ‘template_url’); ?>” Click here</a> to return home page, or you can wait to be redirected in 15 seconds.</h3>

<?php get_sidebars(‘right’); ?>

<?php get_footer(); ?>

This above example will allow the user to land on your 404 error page and then automatically take them to the home page. This will also help users stick around instead of them being left confused and leaving your website. Please make sure to replace home or template url with the exact link where it should redirect.