Big Square Blog is a sandbox about web development, freelancing, and all things WordPress. It is written by Philip Arthur Moore, a traveler and web developer from Houston, Texas.

The Entry Archive

How To Go Down In Style While Getting Your WordPress Blog Dugg, Reddited, Slashdotted, or Farked.

Every serious blogger who uses WordPress as the backbone of their website both yearns and fears for the day that their blog gets Dugg. Digg, with now over one million registered users, can catapult a rather obscure piece of virtual real estate into an instant (albeit oft fleeting) smash hit. And the same applies for websites like Reddit, Slashdot, and Fark.

The benefits of massive traffic increases are obvious. Monetary rewards via online ads come to mind, as well as better page rankings in search engines that translate into organic longevity. However, the problem with many self-hosted WordPress websites is that they go down fast and hard after a sudden bolt of online traffic.

A Bit of Background

While the WordPress plugin WP-Cache acts as a part safety net, part savior to many WordPress-driven blogs on the internet (mine included), the possibility still exists that one day your blog will not be able to handle the load of online traffic that it receives in a short period of time.

WordPress depends on a successful connection between its PHP template functions and your MySQL database. When that connection cannot be established, this is what visitors see:

WordPress Error Establishing Database Connection

There is an obvious stress involved with waiting for a healthy connection to be restored between WordPress and your MySQL database. But what often gets overlooked is the ugliness of WordPress’ database connection error page when it is juxtaposed alongside your otherwise seamless blog theme.

Many casual visitors to WordPress blogs have never even heard of WordPress. Imagine their confusion if a huge WordPress logo accompanied by an error message were to pop up on their desktop monitors.

I may not be able to bulletproof your WordPress/MySQL harmony quotient, but I can offer a more attractive alternative to WordPress’ error page if your website cannot handle getting Dugg, Reddited, Slashdotted, or Farked.

What ControlsThe Error Page

The WordPress include file that controls the output of the database error message is located in the ‘wp-includes’ folder. It is called ‘wp-db.php’. The following is a screenshot of the location of the error code in ‘wp-db.php’:

WordPress Error Code

Further down in the ‘wp-db.php’ file is the snippet of code that tells WordPress to output its error message in the event of a database connection failure. We will be modifying this area of the file. I have included a screenshot below:

Function Bail Message

The Solution

When I began seeking an easy and minimally intrusive way for creating a customized WordPress error page, I stumbled upon this gem of a How-To at All Forces. Their approach involves using the PHP command include(). For my purposes, this approach fell a bit short but in its entirety directed me towards another solution that I feel is just as effective, if not better.

I will be using the PHP header() command to completely redirect users to a separate, custom error page on my website. This approach allows me to implement some very small CSS tweeks on my error page and keep everything organized in a suitable fashion for my taste.

Step One

The first step that you will need to complete before even touching your ‘wp-db.php’ file is to create a custom error page. I will leave this up to you. You may decide to keep your error page in line with the theme of your WordPress blog, but I suggest that you err on the side of caution when doing so.

WordPress database connection errors that come as a result of a massive influx of blog traffic mean that your website is probably outputting a ton of its allocated monthly bandwidth. I am on a (gs) Grid Server package by Media Temple and enjoy a hefty amount of bandwidth, but even so, I don’t want to throw any unnecessary bytes away. It is for this reason that I endorse a minimalist approach to your WordPress database error page.

After creating your error page, place it anywhere you like on your web server. I have decided to place my error page under the root directory of www.bigsquaredot.com to keep everything organized the way that I like it. Any location is fine, really.

Step Two

The second step is to insert a few lines of code above the ‘if ( !this->show_errors )’ PHP conditional statement in the ‘wp-db.php’ file to redirect your users to the custom error page that you have created (click here to view the error page that I have created).

WordPress Error Code Solution

The first two lines of code are the work of Melvin Rivera of All Forces. The first line documents the actual error that occurs if your database connection error page is called, while the second line sends an email to you if your blog connection ceases to run properly.

The third line of code redirects the visitor to your customized WordPress ‘error establishing a database connection’ page. The reason I am using this method and not the include() command is because images called by my CSS code did not display while using the include() command.

The Third And Final Step

The third step is to temporarily rename your database field value in the ‘wp-config.php’ file (located in your blog’s root directory) to see if your modifications are working properly. I suggest either doing this on a locally installed copy of WordPress on your computer or waiting until it is very late at night when very few visitors are coming to your website.

Conclusion

Ultimately, outside of WP-Cache you can do little to control a complete tidal wave of traffic to your blog. In the event that your WordPress blog faces hit after hit of traffic and your MySQL database cannot keep up, you will want to create a customized error page.

For the sake of your visitors and the seamless appearance of your website, make these very small alterations to your ‘wp-db.php’ file and create a snazzy error page that contains more valuable information to your visitors than ‘error establishing a database connection’, care of WordPress.

Post Date: March 16, 2007

2 Responses to “How To Go Down In Style While Getting Your WordPress Blog Dugg, Reddited, Slashdotted, or Farked.”

  1. Pardis:

    September 22nd, 2007 at 7:25 am

    Hi,

    Thanks a lot for your post. It’s pretty useful, given the fact that our database server needs a restart almost twice a month!

    By the way, great choice of title “Going down with style …”.

  2. CantStopTheSignal:

    June 3rd, 2008 at 9:25 pm

    Why can’t cached pages be shown in lieu of error pages, if they exist? There’s a perfectly good copy of the page in cache, why show an error page?

    The wp-cache engine would just need to include a comment tag in the html source that a database error occured and the page is from cache. If the cache is expired, then show it anyway, but also output the 500 header. Otherwise 200 header if the page isn’t otherwise expired.

Leave a Reply

Quicktags: