Technically Feasible

Templated 410 Error Pages in WordPress

Likeness of Michael Oldroyd
Michael Oldroyd
⚠️ This content was imported from a previous incarnation of this blog, and may contain formatting errors

The 4xx class of status code is intended for cases in which the client seems to have erred. Except when responding to a HEAD request, the server should include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method. User agents should display any included entity to the user. These are typically the most common error codes encountered while online.

via Wikipedia

410 is a less common status code to encounter. Back in the days of static web-sites, a physical file was either found or wasn't. If it wasn't, then it had been deleted (or never existed), which resulted in a 404 Not Found. For 410 to work, there had to be a record of what had been there previously and then been subsequently removed. Content usually moves, so redirects are suitable, but when removed it's usually a 404 page you will find.

I just installed '410 for WordPress'. This handles the record of pages that are 'gone'. I added a link to the list, but was disappointed when I was presented by a white page exclaiming 'Sorry, the page you requested has been permanently removed.'. It's commonplace to template the 404 pages of sites (It's built into WordPress themeing), and a shame that WordPress just doesn't do the same when it encounters any other error code.

If you inspect the source code for the plug-in, it fires an action 'wp_410_response'. All we really need to do is hook into this action, so open up the functions.php and add the following code:

add_action('wp_410_response','load_410_template',1,0);
function load_410_template() {
require('410.php');
exit(0);
}

Then create the 410.php file. I simply copied my 404.php, and changed the wording to match the context. That's two error codes covered!

Enjoy :).

Update: The 0.5 release of the 410 for WordPress plug-in detects the 410.php file within the active template, so for this version and above there is no need to add the event hook into your template's functions.php. Just add the file and your covered.

Image of me

Michael Oldroyd

Michael is a Software Engineer working in the North West of England.