How to activate maintenance page (WP Maintenance Mode) programmatically

How to activate maintenance page (WP Maintenance Mode) programmatically

One of the WP Maintenance Mode users asked the support team how can the maintenance page be activated/deactivated programmatically. In case you were wondering the same thing, this is the solution:

First, create a function and add it where needed.


/**
 * Trigger activation/deactivation of the maintenance mode
 * 
 * @param int $status
 * @return
 */
function wpmm_custom_trigger($status) {
	if (!in_array($status, array(0, 1))) {
		return;
	}

	if (!class_exists('WP_Maintenance_Mode')) {
		return;
	}

	// get settings
	$settings = WP_Maintenance_Mode::get_instance()->get_plugin_settings();
	
	// set status
	$settings['general']['status'] = $status;
	$settings['general']['status_date'] = date('Y-m-d H:i:s');
	
	// save settings
	update_option('wpmm_settings', $settings);
	
	// here you should have a clear cache mechanism if you have a cache plugin installed and activated...
        // starting point: https://github.com/Designmodocom/WP-Maintenance-Mode/blob/master/includes/classes/wp-maintenance-mode-admin.php#L414
}

Then use the function like this:


// enable the maintenance mode
wpmm_custom_trigger(1);

// disable the maintenance mode
wpmm_custom_trigger(0);
Leave a Reply

Get your fix of
all cool things WordPress

DigitalOcean hosting coupon 10$ coupon

for DigitalOcean hosting.
Sammy-approved.

Get this deal