2

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.

2
  • @Rarst native maintenance blocks process before loading any plugin or theme. Look at the source code wp-settings.php file. Maintenance mode is checked on 55 line and plugins are loaded on 168, 175 and 209 lines. So there is no way to worry about plugins run. Commented Jan 4, 2014 at 9:45
  • 1
    @EugeneManuilov ops, my mistake! I even did take quick look at the code, I blame friday. :)
    – Rarst
    Commented Jan 4, 2014 at 11:22

1 Answer 1

3

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.

5
  • 3
    I don't get preachy about these things if it's for individual use, but for distributed plugins, it would be good to use the WP_Filesystem methods rather than file_put_contents() and co. directly. Commented Jan 3, 2014 at 15:33
  • Thanks! Super answer and just what I was looking for. Although, Rarst's excellent insight above also means it won't quite do what I was hoping and I'll probably need to find a better way to pause the other plugin. Props you you both though!
    – jb510
    Commented Jan 4, 2014 at 2:28
  • 1
    @jb510 Rarst's insight is not so excellent as you treat it, because if native maintenance mode is enabled, then no plugins/themes are loaded and there is no need to pause anything. It means that you don't need to find better way. Commented Jan 4, 2014 at 9:47
  • 1
    @jb510 yes, sorry, never mind me - that was wrong. :)
    – Rarst
    Commented Jan 4, 2014 at 11:23
  • Cool, well I used it and it seems to have done the trick in avoiding the race condition. TY all.
    – jb510
    Commented Jan 5, 2014 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.