Is it possible to us WP Core Maintenance Mode for short pause while a plugin runs?

I’ve written a plugin to manually (admin button push) delete several hundred posts that are created by another plugin. That other plugin creates posts based on an external API.

I’m running into a situation where occasionally the API plugin is starting to creating new posts midway through the deletion.

I’m wondering if there is a way to use WP’s core maintenance mode for just a few seconds to pause everything else on the site from running while the delete runs?

I’ve looked at update-core.php and update.php but it wasn’t quite obvious to me what it was doing to initiate and terminate maintenance mode. I was kind of hoping there was a function to set maintenance mode and another to clear maintenance mode, but I’m not seeing it. Open to other ways to avoid this race condition as well.

1 Answer
1

It could be easily done by creating .maintenance file in the root of your WordPress instance. Your script should look like this:

// create maintenance file before starting the long lasting process
file_put_contents( ABSPATH . '.maintenance', '<?php $upgrading = ' . time() . ';' );

// do stuff ...

// after finishing working on your stuff remove maintenance file
unlink( ABSPATH . '.maintenance' );

WordPress checks whether or not maintenance file exists in the root and if it does, then checks $upgrading variable to show maintenance message only for 10 minutes.

By default WordPress shows standard message during maintenance mode. It contains following text:

Briefly unavailable for scheduled maintenance. Check back in a minute.

If you want to run your own script and generate unique maintenance output, then you can create maintenance.php file within your wp-content folder. WordPress loads that file if it exists and stops executing a process after calling the file.

Leave a Comment