Custom error pages for WordPress

December 29th, 2003
WordPress Hack

Here is a question I came across at the WordPress forums:

Let’s say there’s an errant link on my site (ex: /index.php?cat=99, where there’s no such category) Currently, it just returns the main template with the content area blank. Is there a way to have it display a custom message? Not a custom 404 message…that I can do for my site. I’m talking about when there’s an invalid string in WP. Per my example, if there’s a link to /index.php?cat=99, index.php is a valid page (my WP template), so there is no 404 error. However, there’s no category 99 in my WP installation, so it returns an empty template (well, the template has everything it should, but the content area is blank.) Go ahead, try it on you own site: enter a non-existant cat and see what happens. Now, try it again here on the WP site. Try this: See, it doesn’t return a blank template, but a “stock” message….

Here was my answer, I am blogging this because someone else might find it useful.

Here is something you could try.
In your blog.header.php (in the root of your WordPress folder) find the line below:
$posts = $wpdb->get_results($request);

Add this just below that line:
if ($posts == ” && $s == ”) {
$cat = ”;
include($abspath . ‘blog.header.php’);

This way, every errant request coming to your blog will be greeted with the default blog template with all posts from all categories.You could modify this to get other output. (Single category, search page, default page, etc…)

PS: Those are two single quotes, not a double quote.




  1. alex says:

    You should probably account for search results… you don’t want an empty search result returning all posts.

  2. Mark (14 comments.) says:

    Good point! Fixed it…I think!

  3. alex says:

    isset($_REQUEST[“s”]) is usually how I check, but that gets translated to $s pretty early so I think that is fine.

Obviously Powered by WordPress. © 2003-2013

page counter