WP_Upgrader::maintenance_modepublicWP 2.8.0

Toggles maintenance mode for the site.

Creates/deletes the maintenance file to enable/disable maintenance mode.

Method of the class: WP_Upgrader{}

No Hooks.

Returns

null. Nothing (null).

Usage

$WP_Upgrader = new WP_Upgrader();
$WP_Upgrader->maintenance_mode( $enable );
$enable(true|false)
True to enable maintenance mode, false to disable.
Default: false

Notes

  • Global. WP_Filesystem_Base. $wp_filesystem WordPress filesystem subclass.

Changelog

Since 2.8.0 Introduced.

WP_Upgrader::maintenance_mode() code WP 6.9.1

<?php
public function maintenance_mode( $enable = false ) {
	global $wp_filesystem;

	if ( ! $wp_filesystem ) {
		if ( ! function_exists( 'WP_Filesystem' ) ) {
			require_once ABSPATH . 'wp-admin/includes/file.php';
		}

		ob_start();
		$credentials = request_filesystem_credentials( '' );
		ob_end_clean();

		if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
			wp_trigger_error( __FUNCTION__, __( 'Could not access filesystem.' ) );
			return;
		}
	}

	$file = $wp_filesystem->abspath() . '.maintenance';

	if ( $enable ) {
		if ( ! wp_doing_cron() ) {
			$this->skin->feedback( 'maintenance_start' );
		}

		// Create maintenance file to signal that we are upgrading.
		$maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
		$wp_filesystem->delete( $file );
		$wp_filesystem->put_contents( $file, $maintenance_string, FS_CHMOD_FILE );
	} elseif ( $wp_filesystem->exists( $file ) ) {
		if ( ! wp_doing_cron() ) {
			$this->skin->feedback( 'maintenance_end' );
		}

		$wp_filesystem->delete( $file );
	}
}