Optimizing WordPress 404’s

One of the great things about WordPress is how 404 error pages are handled. If a page isn’t found then you can show a proper dynamic error page giving the user things to do – this removes a lot of the traditional problems with 404 pages, ie the dead end syndrome.

404 is the server error code that is generated for content that doesn’t exist.

However these new 404 pages are a lot slower than normal 404’s, they require database contact, and PHP processing so if you have a lot of them they can slow down your server. Most times the benefits outweigh the negatives however there is one time when this isn’t the case.

Images and other media that are not loaded, do not need a full 404 page. If you’re linking to an image that doesn’t exist then you could create a lot of extra work for your web server.

How to make it work

To stop the 404 page from executing fully I look at the url and work out if it’s a url for a blocked file type (jpg, css, js, etc). If the file is in the list of bad file types then it gets stopped and a message is displayed.

function bm_404Response() {
	header('HTTP/1.1 404 Not Found');
	if (!empty($_SERVER['REQUEST_URI'])) {
		$fileExtension = strtolower(pathinfo($_SERVER['REQUEST_URI'], PATHINFO_EXTENSION));
	} else {
		$fileExtension = '';
	$badFileTypes = array(
		'css', 'txt', 'jpg', 'gif', 'rar', 'zip', 'png', 'bmp', 'tar', 'doc', 'xml', 'js',
	$badFileTypes = apply_filters('bm_404BadFileTypes', $badFileTypes);
	if (in_array($fileExtension, $badFileTypes)) {
		echo 'error - file does not exist';

Where to use it

This function should be added to your themes functions.php file. To execute it I placed it at the top of my 404.php template page, however in hindsight I could probably also use an action instead and may just tweak my theme to use that instead. There’s nothing like writing about something to make the problems clear! 🙂

Do you have any ideas for other things I could do to improve this code? Do you think it’s a good idea?

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

11 Oct 2009

How to Make TimThumb Work With WordPress Multisite

TimThumb is a popular image resizing script that was created for Mimbo Pro – but it’s never worked properly with WordPress MU, so I wanted to change that.The reason it doesn’t work is quite simple. Because of the way WordPress...
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...
13 Jul 2009

Fussing over the (web design) details

Having spent the last couple of months (on and off) messing around with the new Binary Moon design I am now at the stage where I want to say it’s finished and move on to something else.There’s a well known...
28 Aug 2008

WordPress 2.7 and Crazyhorse

Last weekend I spent a few days away from WordPress and work in general to spend some quality time with the girlfriend. When I got back and updated my local copy of WordPress (via svn) I was surprised to see...
04 Nov 2010

TimThumb Troubleshooting Secrets

I often get asked questions about TimThumb and why it doesn’t work in certain situations. I can generally tell what is wrong with the script within about 60 seconds of being sent a demo url. Below are my top tips...