Schedule WordPress Auto-Updates to only run during business hours

We've turned on WordPress Auto-Updates (yey!), but, just in case this does break anything, we'd like to only run Auto-Updates during daylight hours (e.g. 09:00 - 17:30).

We have the "on init" WP-Cron disabled, and are calling it from the servers crontab every 10 minutes.

How can adapt the AutoUpdate, so all updates are run during these times? I've looked at hooks for WP-Cron, but got a little lost.

Additionally - is there a hook that fires once an auto-updated is complete? Our code is in source control, and I'm looking at ways to 'auto-commit' a new branch into our code after an update (rather than having to do it manually).

(The duplicate question doesn't provide any guidance into the hooks to check - simply suggesting to edit the Database - it also talks about WP Cron being fired 'on load' - rather than running on a dedicated cron).

Cheers, Darren

Topic wp-cron cron automatic-updates hooks plugins Wordpress

Category Web


// An example to allow plugin auto update between "friday 6pm et monday 12am" :
$updates_suspended = !((date('N') == 5 && date('G') > 17) || in_array(date('N'), array(6,7)) || (date('N') == 1 && date('G') < 12)(;
define( 'AUTOMATIC_UPDATER_DISABLED', $updates_suspended );

the value of date('N') is an integer who is the day of the week (1 to monday and 5 to friday) the value of date('G') is an integer who is the hour of day If you just want handle with hour of day, use only date('G') Example : to allow update plugin auto every day only after 11pm and before 6am :

$updates_suspended = !((date('G') < 6 || date('G') > 22));
define( 'AUTOMATIC_UPDATER_DISABLED', $updates_suspended );

This code goes in wp-config.php, it have been tested and it works.

More details on dates here : https://www.php.net/manual/fr/function.date.php


Based on "BenCole" answer:

date() is affected by runtime timezone changes which can cause date/time to be incorrectly displayed. better to use WordPress native current_datetime() that "retrieves the current time as an object with the timezone from settings".

        $current_time = current_datetime()->format( 'Hi' );

        $disable_updater = ( $current_time > 900 || $current_time < 1730 );

        define( 'AUTOMATIC_UPDATER_DISABLED', $disable_updater );

This one is actually surprisingly simple; add this to your wp-config.php file and all automatic updates will be blocked when outside of the specified hours:

// Suspend updates when outside of business hours, 9:00 AM to 5:30 PM
$updates_suspended = (date('Hi') < 0900 || date('Hi') > 1730);

define( 'AUTOMATIC_UPDATER_DISABLED', $updates_suspended );

You can also use these other constants and filters to control which automatic updates are allowed to run if you need to make this more specific.

Make sure you use the server timezone when you set this. The function date() might be configured to use a timezone that is different from your local timezone.

About

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