public
Last active

Backup scheduler for WordPress Automatic Updates

  • Download Gist
backup-scheduler.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<?php
 
function my_backup_scheduler( $event ) {
// 'wp_maybe_auto_update' is the cron hook for the auto update process
if ( 'wp_maybe_auto_update' !== $event['hook'] )
return;
 
wp_schedule_single_event( $event['timestamp'] - 5 * MINUTE_IN_SECONDS, 'my_backup' );
}
add_filter( 'schedule_event', 'my_backup_scheduler', 10, 1 );
 
function my_backup() {
$will_probably_update = false;
 
$au = new WP_Automatic_Updater;
 
wp_update_plugins(); // Check for Plugin updates
$plugin_updates = get_site_transient( 'update_plugins' );
if ( $plugin_updates && !empty( $plugin_updates->response ) ) {
foreach ( array_keys( $plugin_updates->response ) as $plugin ) {
if ( $au->should_update( 'plugin', $plugin, WP_PLUGIN_DIR ) ) {
$will_probably_update = true;
break;
}
}
}
 
if ( ! $will_probably_update ) {
wp_update_themes(); // Check for Theme updates
$theme_updates = get_site_transient( 'update_themes' );
if ( $theme_updates && !empty( $theme_updates->response ) ) {
foreach ( array_keys( $theme_updates->response ) as $theme ) {
if ( $au->should_update( 'theme', $theme, get_theme_root( $theme ) ) ) {
$will_probably_update = true;
break;
}
}
}
 
if ( ! $will_probably_update ) {
wp_version_check(); // Check for Core updates
$core_update = find_core_auto_update();
if ( $core_update && $au->should_update( 'core', $core_update, ABSPATH ) )
$will_probably_update = true;
}
 
if ( $will_probably_update ) {
// There are updates, so you should do your backup
}
}
add_action( 'my_backup', 'my_backup' );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.