Best Way to Enter Maintenance Mode Programmatically

I need the site to be in maintenance mode while I complete a cron job within a plugin.

I have solved this by creating a .maintenance file in my plugin directory and using the following code in my cron job:

$plugin_dir = ABSPATH . 'wp-content/plugins/my_plugin/';
$base_dir = ABSPATH;
$static_maintenance_file = $plugin_dir . '.maintenance';
$new_maintenance_file = $base_dir . '.maintenance';
copy( $static_maintenance_file, $new_maintenance_file );

// do the business end of the cron job

unlink( $new_maintenance_file );

It works as designed. However, it feels a little dirty. Is there anything particularly wrong with the method that I used? And is there a better, more WP friendly way of accomplishing this?

Topic cron maintenance Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.