Using WordPress ‘find_core_auto_update()’ PHP function

The find_core_auto_update() WordPress PHP function fetches the most suitable, enabled Auto-Update for your WordPress core. If there are multiple updates available, it smartly chooses the best one based on your current installation settings.

Usage

$best_update = find_core_auto_update();

In this example, the function find_core_auto_update() is being called, and the output (the best available update) is being stored in the variable $best_update.

Parameters

  • This function does not accept any parameters.

More information

See WordPress Developer Resources: find_core_auto_update()

This function is a part of WordPress Core and is primarily used for managing updates. The source code for this function can be found in the wp-admin/includes/update.php file of the WordPress installation.

Examples

Checking if an update is available

$best_update = find_core_auto_update();
if ($best_update) {
    echo "An update is available: " . $best_update->current;
} else {
    echo "No updates available";
}

This code checks if there’s an update available and outputs the version number of the update if it exists.

Only proceed with update if major release

$best_update = find_core_auto_update();
if ($best_update && strpos($best_update->current, '1.') === 0) {
    // Code to proceed with the update
}

This code checks if the update is a major release (version starts with ‘1.’) before proceeding.

Logging update info

$best_update = find_core_auto_update();
if ($best_update) {
    error_log("Update available: " . $best_update->current);
} else {
    error_log("No updates available");
}

This code logs the available update information to the error log.

Notifying admin of update via email

$best_update = find_core_auto_update();
if ($best_update) {
    wp_mail('[email protected]', 'Update available', 'Update to version ' . $best_update->current . ' is available.');
}

This code sends an email to the admin when an update is available.

Auto-update on major release

$best_update = find_core_auto_update();
if ($best_update && strpos($best_update->current, '1.') === 0) {
    // Code to auto-update WordPress
}

This code triggers an auto-update when a major release is available.