• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Custom WordPress database error pages ...

WordPress 2.3.2 (we're on 2.3.3 at the moment) introduced a nice new feature which has been on my todo list for quite a while, and I've now finally gotten around to: the possibility to create a custom database error page.

View More Info

WordPress 2.3.2 (we are on 2.3.3 at the moment) introduced a nice new feature which has been on my todo list for quite a while, and I have now finally gotten around to: the possibility to create a custom database error page. The idea is simple, you upload a file called db-error.php to your wp-content directory, and that file will be shown when the database connection fails. There is a few neat things you can do with that, and some things you should keep in mind.

Quite a few blogs have already discussed this feature, and they all forgot one thing (that is not properly documented anywhere, so you can really blame them): their error pages don give an HTTP 500 Internal Server Error header, but a normal "200 OK" header. If you do nothing, the default WordPress error page does give an error 500, but the db-error.php page doesn . This means that should your database fail, and someone would open your front page at example.com, he would be served that error page with a 200 OK header. If that someone were a search engine, that is what it would index... If the search engine encounters a 500 error, it will not index that page and just wait for your server to be fixed again. That means not making sure it sends a 500 header is a huge risk.

The fix is simple, you just add a single line to your db-error page:
view plaincopy to clipboardprint?

   1. header("HTTP/1.0 500 Internal Server Error"); 

header("HTTP/1.0 500 Internal Server Error");

Now once you have this error page, there is all sorts of cool things you can do. For instance, I made my error page send out an email to me first, so I can miss it when my database fails. The code for that is quite simple:
view plaincopy to clipboardprint?

   1. $mailto     = "Joost de Valk <no @spamfor.me>"; 
   2. $mailfrom   = "no@spamfor.me"; 
   3. if ($_SERVER[REQUEST_URI] != "/wp-content/db-error.php") { 
   4.     $headers    = "From: ".$mailfrom." ". 
   5.               "X-Mailer: PHP/".phpversion()." ". 
   6.               "X-Priority: 1 (High)"; 
   7.     $message    = "Go fix it! ". 
   8.               "It broke when someone tried to open this page: http://".$_SERVER[SERVER_NAME].$_SERVER[REQUEST_URI]." ". 
   9.               "Best, ". 
  10.               "Your WordPress installation"; 
  11.     $subject    = "DB error at ".$_SERVER[SERVER_NAME]; 
  12.     mail($mailto,$subject,$message,$headers); 
  13. } 

$mailto     = "Joost de Valk <no @spamfor.me>";
$mailfrom     = "no@spamfor.me";
if ($_SERVER[REQUEST_URI] != "/wp-content/db-error.php") {
    $headers     = "From: ".$mailfrom." ".
              "X-Mailer: PHP/".phpversion()." ".
              "X-Priority: 1 (High)";
    $message    = "Go fix it! ".
              "It broke when someone tried to open this page: http://".$_SERVER[SERVER_NAME].$_SERVER[REQUEST_URI]." ".
              "Best, ".
              "Your WordPress installation";
    $subject    = "DB error at ".$_SERVER[SERVER_NAME];
    mail($mailto,$subject,$message,$headers);
}

You could of course also make it send you a text message, or other fancy things, but then you might want to build in some frequency capping. The 500 header goes below the piece of code above by the way, you might get a "headers already sent" error otherwise. Below that you can just put a static HTML page. Some people advice you to use get_header() and get_footer(), but if there is no database connection, WordPress doesn know what theme it should use. So just copy the code in your header.php, single.php and footer.php and strip out all WordPress stuff, and put that below the code above, and you should be good.

source: joostdevalk

 View Full Story.
Posted at 10:56:13 am | Permalink | Posted in Blogging Tips  How to Blog  

Related Stuff

New WordPress feature, Possibly Related Posts

If you have a blog on WordPress.com, you may have noticed a new feature called Possibly Related that links posts from other blogs in the ...

What Do You Need to Set up a Wordpress Blog

I've found myself answering this question a lot lately, so I figure I should just turn it into a blog post so I can point people to ...

How To Make a WordPress Privacy Policy

Ever since Adsense updated their terms and conditions, requiring publishers to display a privacy policy on their website(s), webmasters and ...

WordPress Single Post Templates

Austin recommends using a filter in your functions.php file as an alternative to the method below. IMO, his suggestion is much simpler and ...

Wordpress Plugin - SuperFast Digg This

SuperFast Digg This is really a super fast social bookmarking plugin, it loads faster than others, scalable and with better performance. It ...

Be the first ... |Add your comment.

Your Comment ...

  Name (required)

  Email (required, hidden)

  Website


About Webloglines

Webloglines is a project offers a comprehensive collection of blogging services, articles, themes and plugins from around the world. Whether you're looking to promote your own blog or find blogs on various topics, this site is for you.


Recent Stuff

New WordPress feature, Possibly Related Posts

What Do You Need to Set up a Wordpress Blog

How To Make a WordPress Privacy Policy

WordPress Single Post Templates

Wordpress Plugin - SuperFast Digg This

Fast-Static-Feed WordPress Plugin


Our Partners

Ajax Projects

Web 2.0 Sites

Webloglines

Human Development Handbook

Software Development Company

Ajaxlines


Search


Topics

  • Blogging Tips (40)
  • Blogs Slides (25)
  • Blogs Websites (22)
  • How to Blog (22)
  • Search Engines (1)
  • WordPress Plugins (14)
  • WordPress Security (4)
  • Wordpress Themes (11)
  • Wordpress Tips (9)

© 2006 www.webloglines.com. All Rights Reserved. Powered by IRange