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");

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?

  • You're right I said minimal but I didn't say none :-) In this case I think I'd use either the site URL override constant, or just a constant of your own if you don't want it to have any effect when the db is attached. – Andy Macaulay-Brook Sep 30 '16 at 18:13
  • Or you could include saber.html instead of redirecting to it – Andy Macaulay-Brook Sep 30 '16 at 18:24
  • Im thinking of this from a plugin development scenario and how I can get a redirect or even the db-error.php file there but that was going to be a second question but Im still testing the db-error.php as this made me want to figure out how to get the URL without the db first. – DᴀʀᴛʜVᴀᴅᴇʀ Sep 30 '16 at 18:26
  • No db means no plugins though – Andy Macaulay-Brook Sep 30 '16 at 18:48
  • @AndyMacaulay-Brook but a plugin can write a file to setup this can it not? Still researching it though – DᴀʀᴛʜVᴀᴅᴇʀ Sep 30 '16 at 18:51

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.


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.

| improve this answer | |
  • Is there no way to set a constant without manually adding it in the wp-config? – DᴀʀᴛʜVᴀᴅᴇʀ Sep 30 '16 at 18:37
  • You need to add the define somewhere. Adding it to wp-config.php is the recommended place to put it so you're not hacking something else in WP core itself. – EAMann Sep 30 '16 at 18:38
  • How would you call the constant in db-error.php? – DᴀʀᴛʜVᴀᴅᴇʀ Sep 30 '16 at 19:42
  • I'd make it be the same thing. If you're loading wp-config.php, keep it stored as WP_HOME or WP_SITEURL then reference it in db-error.php. If you want a non-standard constant, just drop it in db-error and call it whatever you need. A custom, non-standard name won't have any meaning to or impact on WordPress, so it wouldn't need to be in the wp-config file at all. – EAMann Sep 30 '16 at 20:43

Well the DB error has to come from the site requested. So why not just use the Superglobal $_SERVER and get the HTTP host.

header("Location: https://" . $_SERVER['HTTP_HOST'] ); //Redirect to root

exit(); //Exit 
| improve this answer | |

WordPress relies on configuration to "tell" it what's the correct URL is. This might be database option or WP_HOME constant which overrides it. For a custom site it is common practice to set constant in wp-config.php and not bother with DB option altogether.

General case is more complicated since there is no dedicated WP API function (that I can remember) which makes it determine that information without configuration.

The closest routine I can think of in source is when RELOCATE mode is set, which explicitly tells WP that URL configuration is unreliable and should be updated from current state.

The relevant code in wp-login.php does following:

if ( defined( 'RELOCATE' ) && RELOCATE ) { // Move flag is set
    if ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != $_SERVER['PHP_SELF']) )
        $_SERVER['PHP_SELF'] = str_replace( $_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF'] );

    $url = dirname( set_url_scheme( 'http://' .  $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ) );
    if ( $url != get_option( 'siteurl' ) )
        update_option( 'siteurl', $url );

So overall without access to configuration your remaining option is going to $_SERVER context, which is not too reliable.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.