WordPress tips and tricks – custom 404 (error) pages

One of the more underused features in wordpress themes is the ability to create and modify the 404 page – so to try to rectify this I thought I’d run through what I do with 404 pages on Binary Moon (and my other WordPress site). I use this simple technique to try to make errors a bit more and helpful for people who have ended up at the wrong place. I’m hoping that not too many people see the 404 page but just in case here is what I’ve done.

Basically I duplicate a normal page template and name it “404.php”. I then remove “the loop” and add some some static code which explains that there has been an error and a few possible solutions (archives, search).

Finally I use query_posts to add in a new loop which displays links to the 5 most recent articles. Below is the actual code I use on this site for my 404 page (you can see it in action here – https://www.binarymoon.co.uk/9837489)


<?php get_header(); ?>

	<h1>404 - Doh!</h1>

	<p>Something has gone wrong and the page you're looking for can't be found.</p>
	<p>Hopefully one of the options below will help you</p>

	<ul>
		<li>You can search the site using the search box to the left</li>
		<li>You could visit <a href="https://www.binarymoon.co.uk/">the homepage</a></li>
		<li>Maybe what you're looking for is </a><a href="https://www.binarymoon.co.uk/archives/">in the Archives?</a></li>
		<li>Or you could have a look through the recent posts listed below, maybe what you're looking for is there</li>
	</ul>

	<h3>Recent Posts</h3>

	<ul>
	<?php
	query_posts('posts_per_page=5');
	if (have_posts()) : while (have_posts()) : the_post(); ?>
	<li><a href="<?php the_permalink() ?>" title="Permalink for : <?php the_title(); ?>"><?php the_title(); ?></a>
	<?php endwhile; endif; ?>
	</ul>

<?php get_footer(); ?>

Something you may want (need?) to do is to add your new 404 page to your .htaccess file so that when a bad/ non-existant page is accessed the visitor gets your custom 404 instead of your webhosts ugly page. To do this all you need to do is grab your .htaccess file and add the following code.


ErrorDocument 404 /index.php?error=404

That’s it – easy as can be.

Now nobody has an excuse not to have a helpful, attractive 404 page. Of course it doesn’t have to be helpful (but that IS nice) but can also be funny or amusing – maybe you can find some inspiration here at area 404. Be sure to let me know if you have an unusual or unique 404 page as well.

Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).

Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

WordPress News

The latest WordPress updates from the WPBriefs Podcast.

Related Posts

30 Jun 2007

WordPress tips and tricks – Custom Page Templates

Some time ago I posted the first of my tips and tricks for WordPress, and I thought it was about time I posted some more so, to start things off, here is a short tutorial on custom page templates in...
23 Oct 2012

WordPress Pagination with Custom WordPress Queries

I don’t remember the last time I made a WordPress theme that didn’t do all sorts of unique stuff. I’ve never been interested in making standard blog designs and always want to create something a bit different. This throws up...
30 Mar 2010

10 WordPress query_posts tips you probably don’t know

I have written a really brief query_post tutorial before, and it did quite well, but both WordPress and my own skills, have advanced considerably since then. So I thought it would be interesting to revisit the query_posts command and see...
13 Mar 2018

WordPress: The Difference Between is_home and is_front_page

When making WordPress themes there’s 2 functions that are really handy. is_home and is_front_page. I use them all the time in both themes and plugins. But I can never remember the difference between them.is_home vs is_front_pageOn the surface they are...