upgrader_process_complete
Fires when an upgrader finishes. It can be used to perform an action after a plugin, theme, WordPress core, or language pack update.
The hook fires after the updated module's files have already been replaced. When a plugin is updated, for example, it fires after the plugin files have been updated.
This hook does not have to be registered early. It can be used in a theme's functions.php file, for example.
See also upgrader_package_options.
Usage
add_action( 'upgrader_process_complete', 'wp_kama_upgrader_process_complete_action', 10, 2 );
/**
* Function for `upgrader_process_complete` action-hook.
*
* @param WP_Upgrader $upgrader WP_Upgrader instance. In other contexts this might be a Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
* @param array $hook_extra Array of bulk item update data.
*
* @return void
*/
function wp_kama_upgrader_process_complete_action( $upgrader, $hook_extra ){
// action...
}
- $this(WP_Upgrader)
A WP_Upgrader instance.
Depending on the operation, it can be one of these objects:
- $hook_extra(array)
Data about the item being updated.
-
action(string)
Event type.
Default: 'update' -
type(string)
Update process type. Possible values:plugin,theme,translation, orcore. -
bulk(true/false)
Whether this is a bulk update process.
Default: true -
plugins(array)
Paths to the main plugin files, relative to the plugins directory. -
themes(array)
Theme slugs. -
translations(array)
Translation data.-
language(string)
Translation locale. -
type(string)
Translation type. Possible values:plugin,theme, orcore. -
slug(string)
Translation text domain: the theme/plugin slug, ordefaultfor core translations. - version(string)
Theme, plugin, or core version.
-
-
Examples
#1 Plugin update and the data received by the hook
Update Query Monitor from version 3.3.2 to 3.3.3 and write the hook variables to __upgrader_process_complete_info.txt in the site root.
// Upgrade test
add_action( 'upgrader_process_complete', 'my_upgrate_function', 10, 2);
function my_upgrate_function( $upgrader_object, $hook_extra ){
$put = '';
$put .= '$upgrader_object = '. print_r( $upgrader_object, 1 ) ."\n\n\n";
$put .= '$hook_extra = '. print_r( $hook_extra, 1 ) ."\n\n\n";
foreach( $hook_extra['plugins'] as $plugin_rel_path ){
$data = get_plugin_data( WP_PLUGIN_DIR ."/$plugin_rel_path" );
$put .= "$plugin_rel_path data = ". print_r( $data, 1 ) ."\n\n\n";
}
file_put_contents( "{$_SERVER['DOCUMENT_ROOT']}/__upgrader_process_complete_info.txt", $put );
}
The resulting __upgrader_process_complete_info.txt file contains the following data from inside the hook callback:
#2 Display a notice after a plugin update
This mini-plugin demonstrates how to display a notice when a plugin is updated.
It displays different admin notices (see admin_notices): one after the plugin is installed (activated), and another after the plugin is updated using the current hook.
<?php
/**
* Plugin Name: Upgrader Process Example
* Plugin URI: https://catapultthemes.com/wordpress-plugin-update-hook-upgrader_process_complete/
* Description: Just an example of using upgrader_process_complete
* Version: 1.0.0
* Author: Catapult Themes
* Author URI: https://catapultthemes.com/
* Text Domain: wp-upe
* Domain Path: /languages
*/
defined( 'ABSPATH' ) || exit;
add_action( 'upgrader_process_complete', 'wp_upe_upgrade_completed', 10, 2 );
add_action( 'admin_notices', 'wp_upe_display_update_notice' );
add_action( 'admin_notices', 'wp_upe_display_install_notice' );
# On activation, set a transient so we know the plugin was just activated
register_activation_hook( __FILE__, function() {
set_transient( 'wp_upe_activated', 1 );
} );
/**
* This function runs when WordPress completes its upgrade process
* It iterates through each plugin updated to see if ours is included
*
* @param array $upgrader_object
* @param array $options
*/
function wp_upe_upgrade_completed( $upgrader_object, $options ) {
// The path to our plugin's main file
$our_plugin = plugin_basename( __FILE__ );
// If an update has taken place and the updated type is plugins and the plugins element exists
if( $options['action'] === 'update' && $options['type'] === 'plugin' && isset( $options['plugins'] ) ) {
// Iterate through the plugins being updated and check if ours is there
foreach( $options['plugins'] as $plugin ) {
if( $plugin == $our_plugin ) {
// Set a transient to record that our plugin has just been updated
set_transient( 'wp_upe_updated', 1 );
}
}
}
}
/**
* Show a notice to anyone who has just updated this plugin
* This notice shouldn't display to anyone who has just installed the plugin for the first time
*/
function wp_upe_display_update_notice() {
// Check the transient to see if we've just updated the plugin
if( get_transient( 'wp_upe_updated' ) ) {
echo '<div class="notice notice-success"><p>' . __( 'Thanks for updating', 'wp-upe' ) . '</p></div>';
delete_transient( 'wp_upe_updated' );
}
}
/**
* Show a notice to anyone who has just installed the plugin for the first time
* This notice shouldn't display to anyone who has just updated this plugin
*/
function wp_upe_display_install_notice() {
// Check the transient to see if we've just activated the plugin
if( get_transient( 'wp_upe_activated' ) ) {
echo '<div class="notice notice-success"><p>' . __( 'Thanks for installing', 'wp-upe' ) . '</p></div>';
// Delete the transient so we don't keep displaying the activation message
delete_transient( 'wp_upe_activated' );
}
}
Changelog
| Since 3.6.0 | Introduced. |
| Since 3.7.0 | Added to WP_Upgrader::run(). |
| Since 4.6.0 | $translations was added as a possible argument to $hook_extra. |
Where the hook is called
do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );
do_action( 'upgrader_process_complete', $this, array( 'action' => 'update', 'type' => 'core', ) );
do_action( 'upgrader_process_complete', $this, array( 'action' => 'update', 'type' => 'plugin', 'bulk' => true, 'plugins' => $plugins, ) );
do_action( 'upgrader_process_complete', $this, array( 'action' => 'update', 'type' => 'translation', 'bulk' => true, 'translations' => $language_updates_results, ) );
do_action( 'upgrader_process_complete', $this, array( 'action' => 'update', 'type' => 'theme', 'bulk' => true, 'themes' => $themes, ) );
Where the hook is used in WordPress
add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 );
add_action( 'upgrader_process_complete', 'wp_update_plugins', 10, 0 );
add_action( 'upgrader_process_complete', 'wp_update_themes', 10, 0 );
remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
remove_action( 'upgrader_process_complete', 'wp_version_check' );
remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
remove_action( 'upgrader_process_complete', 'wp_update_themes' );
add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 );
add_action( 'upgrader_process_complete', 'wp_update_plugins', 10, 0 );
add_action( 'upgrader_process_complete', 'wp_update_themes', 10, 0 );
add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
remove_action( 'upgrader_process_complete', 'wp_version_check' );
remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
remove_action( 'upgrader_process_complete', 'wp_update_themes' );
add_action( 'upgrader_process_complete', array( $this, 'invalidate_mo_files_cache' ), 10, 2 );