is_blog_installed()WP 2.1.0

Checks if the site is installed. It checks for the presence of the siteurl option and tables in the database. The result is cached.

After checking the siteurl option (if it is not present), the function checks for the presence of tables in the DB. If tables are found, you will see the error __( 'Error establishing a database connection' ). This situation can occur when the options table is corrupted and the siteurl option cannot be retrieved, but the site is still installed. Therefore, if the siteurl option could not be found, the function checks for the presence of tables. If there are no tables in the DB, then the site is not installed.

The function does nothing and always returns true if we are in database repair/restore mode.

1 time — 0.000026 sec (very fast) | 50000 times — 0.07 sec (speed of light) | PHP 7.0.14, WP 4.7

No Hooks.

Returns

true|false.

Usage

if( is_blog_installed() ){
	// the site is installed!
}

Examples

0

#1 Check if the site is installed, before registering the widget

function widgets_init() {

	if ( ! is_blog_installed() ) {
		return;
	}

	register_widget( 'WP_Widget_Black_Studio_TinyMCE' );
}

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 2.1.0 Introduced.

is_blog_installed() code WP 6.9

function is_blog_installed() {
	global $wpdb;

	/*
	 * Check cache first. If options table goes away and we have true
	 * cached, oh well.
	 */
	if ( wp_cache_get( 'is_blog_installed' ) ) {
		return true;
	}

	$suppress = $wpdb->suppress_errors();

	if ( ! wp_installing() ) {
		$alloptions = wp_load_alloptions();
	}

	// If siteurl is not set to autoload, check it specifically.
	if ( ! isset( $alloptions['siteurl'] ) ) {
		$installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" );
	} else {
		$installed = $alloptions['siteurl'];
	}

	$wpdb->suppress_errors( $suppress );

	$installed = ! empty( $installed );
	wp_cache_set( 'is_blog_installed', $installed );

	if ( $installed ) {
		return true;
	}

	// If visiting repair.php, return true and let it take over.
	if ( defined( 'WP_REPAIRING' ) ) {
		return true;
	}

	$suppress = $wpdb->suppress_errors();

	/*
	 * Loop over the WP tables. If none exist, then scratch installation is allowed.
	 * If one or more exist, suggest table repair since we got here because the
	 * options table could not be accessed.
	 */
	$wp_tables = $wpdb->tables();
	foreach ( $wp_tables as $table ) {
		// The existence of custom user tables shouldn't suggest an unwise state or prevent a clean installation.
		if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE === $table ) {
			continue;
		}

		if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE === $table ) {
			continue;
		}

		$described_table = $wpdb->get_results( "DESCRIBE $table;" );
		if (
			( ! $described_table && empty( $wpdb->last_error ) ) ||
			( is_array( $described_table ) && 0 === count( $described_table ) )
		) {
			continue;
		}

		// One or more tables exist. This is not good.

		wp_load_translations_early();

		// Die with a DB error.
		$wpdb->error = sprintf(
			/* translators: %s: Database repair URL. */
			__( 'One or more database tables are unavailable. The database may need to be <a href="%s">repaired</a>.' ),
			'maint/repair.php?referrer=is_blog_installed'
		);

		dead_db();
	}

	$wpdb->suppress_errors( $suppress );

	wp_cache_set( 'is_blog_installed', false );

	return false;
}