Do you know that you can create a custom 404 page for your WordPress theme? It’s very easy to set up and you don’t need a plugin for that. All you need is to create a 404.php file and add that to your theme – if you don’t have that already.

The basic structure of a 404 template will probably look like this:

<?php
/**
 * The template for displaying 404 pages (Not Found)
 *
 * @package WordPress
 * @subpackage Twenty_Thirteen
 * @since Twenty Thirteen 1.0
 */

get_header(); ?>

<div id="primary" class="content-area">
 <div id="content" class="site-content" role="main">

  <header class="page-header">
   <h1 class="page-title"><?php _e( 'Not Found', 'twentythirteen' ); ?></h1>
  </header>

  <div class="page-wrapper">
   <div class="page-content">
    <h2><?php _e( 'This is somewhat embarrassing, isn’t it?', 'twentythirteen' ); ?></h2>
    <p><?php _e( 'It looks like nothing was found at this location. Maybe try a search?', 'twentythirteen' ); ?></p>

    <?php get_search_form(); ?>
   </div><!-- .page-content -->
  </div><!-- .page-wrapper -->

 </div><!-- #content -->
</div><!-- #primary -->

<?php get_footer(); ?>

It works but does it show exactly what you want it to? I love 404 pages, but I don’t really want my visitors to reach a defunct page although it is inevitable that in one way or another they will eventually get a 404 – so why don’t you make it worth their while? A lot of visitors will probably leave your site as soon as they get a 404 error – I know I will. But if you have a 404 page that gives them information and something to look forward to then I am pretty sure they’ll stay for a bit more.

I have a custom 404 that you can check here 404 Page Not Found. If you want to have a separate header or footer for your 404, WordPress has a convenient function for you to use:

<?php is_404(); ?>

This is a boolean function (meaning it returns TRUE or FALSE) so you can use it to add any feature that’s only for your 404 page.

When creating your 404 page, there are 4 guidelines that you should follow (or not):

  • It should be perfectly clear that your visitor has reached the page in error.
  • It should have a clear course of action, you can either have your menu intact, add a search form or bring them back to the homepage.
  • Do not be generic. It does not hurt to make the 404 experience fun for your visitors – they’re already frustrated that they did not go to where they want to go.
  • It should load as quickly as possible.

In my 404 page, the design is inline with my website and if you will check the url for the “Go Here” link – you’ll notice that it changes on refresh. It’s because I’ve configured it to show random links from my blog. You can do the same by adding the following codes to your functions.php file:

<?php
//Show random post shortcode
function kd_rand_post() {
 $args = array(
 'post_type' => 'post',
 'orderby'   => 'rand',
 'posts_per_page' => 1, //You can set this to any number that you want. I just had it at 1
);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {

 $the_query->the_post();
 $string = '<a href="'. get_permalink() .'" title="'. get_the_title() . '">'. get_the_title() . '</a>';

}

/* Restore original Post Data */
wp_reset_postdata();

} else {

 $string = 'no posts found';

}
 return $string;
}
add_shortcode('kd-random-post','kd_rand_post');
?>

You can then add it to your 404.php like so:

<?php echo do_shortcode( '[kd-random-post]' ); ?>

Or even use it inside your posts/pages or widgets:

[kd-random-post]
Disclaimer: The snippets added to this post might not always work for everyone, if anything happens I am not in any way liable. If you don’t have a backup – that is in no way my problem. If this helped you, share and comment. If you can make it better, let me know via the comment section below.