Almost every WordPress theme has a 404 template. Some are better than others, and a few are quite impressive, but on problem that pervades all of them is the fact that editing them requires opening finding and opening the correct template file. This is OK for people who know about HTML and PHP and are familiar with WordPress, but what about technically challenged people (like your Grandma)? They’re not likely to be able to edit a WordPress template, so they will be stuck with whatever you give them. If you create a good template, this may be sufficient, but there will still be people who want to change something.
Thankfully you can provide a much simpler solution for people who don’t want to edit your templates. With this solution, users can edit the 404 page contents just like it were any other page. Here’s how it works.
To use the WYSIWYG editor, the user actually needs to add a page. Of course they don’t want “404″ showing up everywhere they list their pages, so they need to set the Visibility to “Private”. This will remove it from all page listings. The permalink also needs to be specified explicitly. Using “404″ makes sense to me, and is what I use.
Now in the 404 template we want to check whether a 404 page is defined and if it is, display it. Otherwise we should have some default content to display. Here’s a template 404 file which lays out the general idea. Some tweaking might be required to fit the rest of your theme.
< ?php get_header(); ?> <div id="content" class="narrowcolumn"> <?php query_posts('pagename=404' ); ?> <?php if (have_posts()) : the_post(); ?> <div class="post" id="post-<?php echo $id; ?>"> <h2 class="post-title">< ?php echo $post->post_title; ?></h2> <div class="entry"> < ?php the_content(); ?> </div> </div> < ?php else : ?> <!-- your default 404 content --> < ?php endif; ?> <!-- search form, or anything else you always want to show at the bottom --> </div> < ?php get_sidebar(); ?> < ?php get_footer(); ?>
Notice line 3 where we query for the post with page name “404″.
In line 4 we check to see if the “404″ page actually exists and if it does, we load its information with
In line 6 we use
$post-the_title instead of simply
the_title(). This is because WordPress adds “Private: ” to the beginning of the title of private pages when you call
The rest is pretty straight forward. Place any default content (what you want to show if there is no 404 page defined) inside the
else and then anything you want to always display (whether the user has created a ’404′ page or not) after the
That’s it! If you distribute a theme with this feature, just let people know what you’ve done so they can utilize it.
Private Pages can solve to a wide variety of problems. I hope to post more ways to use them soon. For now if you have any other ideas about how they might be used, or a suggestion for improving this example please add it to the comments