Justin Tadlock

Customize your 404 page from the WordPress admin

Most 404 templates in WordPress themes suck. They generally read something like “404 Not Found” and give an extra sentence or two about an error. This is totally unnacceptable to a site visitor that comes across a 404 page.

A site’s 404 page should be unique and reflect what the site owner(s) want. It’s tough for theme developers to know how to create a 404 page since they’re making a theme for thousands of people. The question becomes: How does one create a 404 template that users can customize from within the WordPress admin?

I’m going to show you an extremely simple technique that’ll make both theme developers and users’ lives easier. We will widgetize our 404 templates from this day forward.

Oh, and by the way, the Hybrid theme already uses this method. Really, did you expect anything less from the coolest theme ever?

Register your 404 widget area with WordPress

Like any other widget area, we need to let WordPress know about it. Open your theme’s functions.php file and add this PHP code in:

<?php
    register_sidebar( array(
        'name' => '404',
        'id' => '404',
        'before_widget' => '<div id="%1$s" class="%2$s widget">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>'
    ) );
?>

If you’re releasing this theme publicly, you should localize the widget area name but not the id.

Displaying the widget area on your 404 pages

Open your theme’s 404.php template and add this to the content area or replace it completely:

<?php dynamic_sidebar( '404' ); ?>

It’s as simple as that. Now you can edit your 404 template at any time without having to open your theme files.