Custom error pages for WordPress
If you like this post, please subscribe to our RSS feed to read our new posts every day.
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: http://wordpress.org/index.php?cat=99. 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.
Visitors who read this post, also read:
Related posts from the past
- Lame Internet Explorer script error message
- Configuring WP Permalinks
- Gzip Compression issue in WordPress 1.2 Mingus
- WordPress Theme Releases for 2/27
- Error Management for WordPress Plugins
- WordPress Plugin Releases for 10/15
- Global Variables and the WordPress Loop
- How to Only Retrieve Posts With Custom Fields
- WordPress Wishlist for October
- Safest Way to Include Plugin Code in Themes














Comments RSS
You should probably account for search results… you don’t want an empty search result returning all posts.
[Reply]
alex — 12/30/2003 @ 10:44 amGood point! Fixed it…I think!
[Reply]
Mark (14 comments.) — 12/30/2003 @ 11:15 amisset($_REQUEST["s"]) is usually how I check, but that gets translated to $s pretty early so I think that is fine.
[Reply]
alex — 12/30/2003 @ 10:51 pm