Recently I realized that you could include a db-error.php file in your wp-content directory from “How to monitor server for error establishing a database connection” that would replace the existing WordPress database error message with something custom. I thought about doing a redirect in db-error.php like:

header("Location: http://vader.com/saber.html");
exit();

but I wanted to replace http://vader.com with the site URL so this could be portable but after researching I didn’t see a way to obtain the site URL without the connection and I per discussions I was told you want to do minimal modifications to the wp.config file. Is there a way to get the site URL without a database connection that could be used in the header redirect?

3 Answers
3

One option is setting the site’s URL in the wp-config.php file itself. This effectively overrides the siteurl option that’s otherwise stored in the database, but it also means you can reference the URL without doing a query.

From the Codex:

It is possible to set the site URL manually in the wp-config.php file.

Add these two lines to your wp-config.php, where “example.com” is the correct location of your site.

define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');

After that, using code in a regular theme that checks the siteurl or home option will pull from the constant rather than the database (hence my note on the override above). But in your default error script, you can reference WP_SITEURL directly to build a redirect URL.

Leave a Reply

Your email address will not be published. Required fields are marked *