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';
		die();
	}
}

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?

How was it for you? Let me know on BlueSky or Mastodon

(Please) 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.

Related Posts

13 May 2010

6 Tips to Build Better WordPress Themes

If you want to make WordPress themes, for clients, to release for free or to sell, then there are a lot of factors you need to take into consideration. Below are some hints and tips that should help ease your...
22 Nov 2014

WordPress 4.1 Improvements for Theme Developers

WordPress 4.1 is bringing with it a couple of cool new additions for theme developers. They’re things that are currently a bit messy to implement in themes. For me they are things that I do the same way in all...
26 Nov 2016

Generating a WordPress rtl.css with Gulp

Generating a rtl.css files is something I don’t enjoy doing. I think it’s really important that they are created since a large proportion of the world uses rtl (right to left) languages. But they are a bit of a pain...
02 Feb 2012

WordPress Theme Framework: Less Is More – Or Is It?

I have a conundrum. I want to improve Elemental – my WordPress theme framework. I want to make it lean and fast; but this will involve removing functionality. This in turn means that when people upgrade their themes things may...
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...