Using WordPress ‘automatic_updates_complete’ PHP action

The automatic_updates_complete WordPress PHP action fires after all automatic updates have run.

Usage

add_action('automatic_updates_complete', 'your_custom_function');
function your_custom_function($update_results) {
    // your custom code here
}

Parameters

  • $update_results (array): The results of all attempted updates.

More information

See WordPress Developer Resources: automatic_updates_complete

Examples

Log update results to a file

Logs the update results to a log file in the root directory.

add_action('automatic_updates_complete', 'log_update_results', 10, 1);

function log_update_results($update_results) {
    $log_file = fopen('updates.log', 'a');
    fwrite($log_file, json_encode($update_results));
    fclose($log_file);
}

Send update results via email

Sends the update results to a specified email address.

add_action('automatic_updates_complete', 'email_update_results', 10, 1);

function email_update_results($update_results) {
    $to = '[email protected]';
    $subject = 'Automatic Update Results';
    $message = json_encode($update_results);
    wp_mail($to, $subject, $message);
}

Post update results to a Slack channel

Posts the update results to a specified Slack channel using a webhook.

add_action('automatic_updates_complete', 'slack_update_results', 10, 1);

function slack_update_results($update_results) {
    $webhook_url = 'https://hooks.slack.com/services/your/webhook/url';
    $data = array('text' => json_encode($update_results));
    $options = array(
        'body' => json_encode($data),
        'headers' => array('Content-Type' => 'application/json'),
    );
    wp_remote_post($webhook_url, $options);
}

Store update results in an option

Stores the update results in a WordPress option for later use.

add_action('automatic_updates_complete', 'store_update_results', 10, 1);

function store_update_results($update_results) {
    update_option('my_update_results', $update_results);
}

Display update results in admin notices

Displays the update results as admin notices in the WordPress dashboard.

add_action('automatic_updates_complete', 'show_update_results', 10, 1);

function show_update_results($update_results) {
    set_transient('my_update_results_notice', $update_results, 3600);
}

add_action('admin_notices', 'display_update_results_notice');

function display_update_results_notice() {
    $update_results = get_transient('my_update_results_notice');
    if ($update_results) {
        foreach ($update_results as $result) {
            echo '<div class="notice notice-info is-dismissible">';
            echo '<p>' . $result . '</p>';
            echo '</div>';
        }
    }
}

Log Update Results

Log the results of automatic updates to a custom log file.

add_action('automatic_updates_complete', 'log_update_results');
function log_update_results($update_results) {
    // Specify the log file
    $log_file = WP_CONTENT_DIR . '/update_log.txt';

    // Open the log file for writing
    $file_handle = fopen($log_file, 'a');

    // Write the update results to the log file
    fwrite($file_handle, print_r($update_results, true));

    // Close the log file
    fclose($file_handle);
}

Send Email Notification

Send an email notification with the update results after automatic updates have completed.

add_action('automatic_updates_complete', 'send_update_email_notification');
function send_update_email_notification($update_results) {
    // Email recipient and subject
    $to = '[email protected]';
    $subject = 'WordPress Automatic Update Results';

    // Prepare the email body with the update results
    $message = 'Automatic updates completed. Results:' . PHP_EOL . print_r($update_results, true);

    // Send the email
    wp_mail($to, $subject, $message);
}

Post Update Results to Slack

Post the update results to a Slack channel after automatic updates have completed.

add_action('automatic_updates_complete', 'post_update_results_to_slack');
function post_update_results_to_slack($update_results) {
    // Slack webhook URL
    $webhook_url = 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX';

    // Prepare the Slack message
    $message = 'WordPress Automatic Update Results:' . PHP_EOL . '```' . print_r($update_results, true) . '```';

    // Send the message to Slack
    wp_remote_post($webhook_url, array(
        'body' => json_encode(array('text' => $message)),
        'headers' => array('Content-Type' => 'application/json'),
    ));
}

Custom Update Result Handling

Perform custom actions based on the type of updates and their results.

add_action('automatic_updates_complete', 'custom_update_result_handling');
function custom_update_result_handling($update_results) {
    foreach ($update_results as $result) {
        // Perform custom actions for successful plugin updates
        if ($result->type == 'plugin' && $result->result) {
            // your custom code for successful plugin updates
        }

        // Perform custom actions for failed theme updates
        if ($result->type == 'theme' && !$result->result) {
            // your custom code for failed theme updates
        }
    }
}

Disable Automatic Updates for Specific Plugins

Prevent specific plugins from being updated automatically.

add_filter('auto_update_plugin', 'disable_automatic_updates_for_specific_plugins', 10, 2);
function disable_automatic_updates_for_specific_plugins($update, $item) {
    // Specify the plugin slugs to exclude from automatic updates
    $excluded_plugins = array(
        'plugin-slug-1',
        'plugin-slug-2',
        'plugin-slug-3',
    );
    // Disable automatic updates for the specified plugins
    if (in_array($item->slug, $excluded_plugins)) {
        return false;
    }
return $update;
}

Log Successful Updates Only

Log only successful updates to a custom log file.

add_action('automatic_updates_complete', 'log_successful_updates');
function log_successful_updates($update_results) {
    // Specify the log file
    $log_file = WP_CONTENT_DIR . '/successful_updates_log.txt';

    // Open the log file for writing
    $file_handle = fopen($log_file, 'a');

    // Loop through update results and log only successful updates
    foreach ($update_results as $result) {
        if ($result->result) {
            fwrite($file_handle, print_r($result, true));
        }
    }

    // Close the log file
    fclose($file_handle);
}