Using WordPress ‘do_core_upgrade()’ PHP function

The do_core_upgrade() WordPress PHP function is used to upgrade the WordPress core display.

Usage

The function is quite simple to use. You just call it and pass a boolean value indicating whether or not to reinstall. For instance:

do_core_upgrade(false);

This line will upgrade the WordPress core without reinstalling it.

Parameters

  • $reinstall (bool) – Optional. This parameter controls whether the WordPress core is reinstalled or not. The default value is false.

More information

See WordPress Developer Resources: do_core_upgrade()

This function has been a part of WordPress core since version 2.7. It is not deprecated and continues to be actively maintained. You can find its source code in the file wp-admin/includes/update-core.php.

Related functions include update_core(), which automatically updates WordPress to the latest version.

Examples

Basic Usage

In this example, we’re updating WordPress without reinstalling it.

do_core_upgrade(false);

Reinstalling WordPress

If you’d like to reinstall WordPress while updating, pass true.

do_core_upgrade(true);

Conditional Reinstallation

In some scenarios, you might want to conditionally decide whether to reinstall or not. In this case, we’re using a variable $shouldReinstall.

$shouldReinstall = get_option('should_reinstall'); // Assume this option returns true or false
do_core_upgrade($shouldReinstall);

Using within a function

You might want to use do_core_upgrade() inside a function to encapsulate the upgrade logic.

function upgrade_my_site($reinstall = false) {
    do_core_upgrade($reinstall);
}
upgrade_my_site(true);

Tied with a WordPress action

This is a more practical example where the upgrade process is tied to a WordPress action, such as ‘init’.

add_action('init', function() {
    if (current_user_can('update_core')) {
        do_core_upgrade(false);
    }
});

In this example, we’re hooking into WordPress’s initialization process and, if the current user has the capability to update the core, we proceed with the upgrade.