Alter how often WordPress Auto-Updates Plugins

From my understanding of how WordPress updates core and plugins is that every 12 hours it goes out and looks for updates. When does that time get set? 12 hours from initial installation? I ask this because using the plugin auto-update filter add_filter( 'auto_update_plugin' ); you could theoretically only allow plugins to update between certain times but if that certain time does not fall into that 12 hour window then plugins will never be updated.

Thus leaving the question in my title which is How would one go about altering how often WordPress looks for updates or how to specify a specific time when to auto-update?

For those interested, here’s the filter I’ve been messing with, checks to see IF it’s Monday – Friday 8am – 5pm:

function maybe_update_plugins(){
    date_default_timezone_set('Your Timezone Here');
    $localAssoc = localtime(time(), true);
    $update = false;

    /***
    /* tm_wday[0] = Sunday
    /* tm_wday[6] = Saturday
    ***/
    if(
      $localAssoc['tm_wday'] > 0 && $localAssoc['tm_wday'] < 6 &&
      $localAssoc['tm_hour'] > 7 && $localAssoc['tm_hour'] < 17
    ){
        $update = true;
    }

    return $update;
}
add_filter( 'auto_update_plugin', 'maybe_update_plugins' );

Edit As a neat sidenote, it does look like this filter is run each time a plugin updates, multiple plugin updates, multiple runs of the filter

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You are correct, WordPress checks for updates to core and plugins every 12 hours, but a better way to word it would be: it checks updates if last update was more than 12 hours ago.

The 12 hour setting is hard codded in wp-includes/update.php

The last updated dates are stored in wp_options table and the options are:

_site_transient_update_core
_site_transient_update_plugins
_site_transient_update_themes

Because this check does not happen precisely after 12 hours, but rather next time the condition is met (at least 12 hours has passed) then you will not miss the update.

Method 2

The relevant functions wp_update_plugins() and wp_maybe_auto_update() are hooked to the wp-cron events of same name, running on twice daily schedule.

Logically the initial schedule will start to tick from the time of first run. Due to wp-cron implementation (not being real cron and trigered by site visits rather than server clock) it will also “drift” whenever actual wp-cron fires past the time it’s due at.

Plugin update checks are also triggered more often in certain admin areas, like plugin list (so that you are less likely to be looking at stale information).

On top of my head I don’t quite see elegant way to put it into specific time corridor. Changing schedules are pretty easy, account for drift is not quite.

If this is in any way misison–critical requirement then it might better be handled with real server cron and external tool for actual update, such as wp-cli update functionality or managing plugins via Composer.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x