dead_db() WordPress Function

The dead_db() function is used to gracefully shut down a WordPress database. This is useful for when a database needs to be taken offline for maintenance or when a website is being migrated to a new server. When called, the function will first attempt to close any active database connections. It will then delete all database tables, leaving only the core WordPress tables intact. Finally, it will change the WordPress database prefix, effectively rendering the database unusable.

dead_db() #

Load custom DB error or display WordPress DB error.


Description

If a file exists in the wp-content directory named db-error.php, then it will be loaded instead of displaying the WordPress DB error. If it is not found, then the WordPress DB error will be displayed instead.

The WordPress DB error sets the HTTP status header to 500 to try to prevent search engines from caching the message. Custom DB messages should do the same.

This function was backported to WordPress 2.3.2, but originally was added in WordPress 2.5.0.


Top ↑

Source

File: wp-includes/functions.php

function dead_db() {
	global $wpdb;

	wp_load_translations_early();

	// Load custom DB error template, if present.
	if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
		require_once WP_CONTENT_DIR . '/db-error.php';
		die();
	}

	// If installing or in the admin, provide the verbose message.
	if ( wp_installing() || defined( 'WP_ADMIN' ) ) {
		wp_die( $wpdb->error );
	}

	// Otherwise, be terse.
	wp_die( '<h1>' . __( 'Error establishing a database connection' ) . '</h1>', __( 'Database Error' ) );
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.2Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More