Filter Since 2.8.0 uncanny-automator-pro

upgrader_post_install

Filter the installation response after the installation has finished. Filters the installation response after a plugin or theme has finished upgrading, providing access to success status and details.

add_filter( 'upgrader_post_install', $callback, 10, 3 );

Description

Fires after a plugin or theme installation completes. Developers can inspect and potentially modify the installation response (boolean indicating success/failure) and access detailed installation results. This hook is ideal for performing post-installation cleanup, logging, or triggering subsequent actions.


Usage

add_filter( 'upgrader_post_install', 'your_function_name', 10, 3 );

Parameters

$response (bool)
Installation response.
$hook_extra (array)
Extra arguments passed to hooked filters.
$result (array)
Installation result data.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of using the 'upgrader_post_install' filter hook to perform actions after an upgrade.
 *
 * This function will log information about the upgrade and potentially
 * trigger a custom notification if the upgrade was successful and involved
 * a specific plugin.
 *
 * @param bool  $response   The installation response.
 * @param array $hook_extra Extra arguments passed to hooked filters.
 * @param array $result     Installation result data.
 *
 * @return bool|WP_Error The modified response.
 */
function my_custom_upgrade_handler( $response, $hook_extra, $result ) {
	// Ensure we're dealing with a plugin upgrade and not a theme or core upgrade.
	if ( isset( $hook_extra['plugin'] ) ) {
		$plugin_slug = $hook_extra['plugin'];
		$plugin_name = basename( dirname( $plugin_slug ) ); // Get the plugin folder name

		// Log the upgrade event for debugging or tracking.
		error_log( "WordPress upgrade completed for plugin: {$plugin_name}. Response: " . var_export( $response, true ) );

		// Check if the upgrade was successful (response is true).
		if ( true === $response ) {
			// You can access detailed results from the $result array.
			// For example, to get the new version:
			if ( isset( $result['new_version'] ) ) {
				error_log( "Plugin {$plugin_name} upgraded to version {$result['new_version']}." );

				// Example: Trigger a custom notification if a specific plugin was upgraded.
				if ( 'my-critical-plugin/my-critical-plugin.php' === $plugin_slug ) {
					// In a real-world scenario, you might use wp_mail() or a custom notification system.
					error_log( "Critical plugin 'my-critical-plugin' has been successfully upgraded!" );
					// You could also add logic here to clear caches, flush rewrite rules, etc.
				}
			}
		} else {
			// Handle upgrade failures, though the original filter logic already checks for WP_Error.
			error_log( "WordPress upgrade failed for plugin: {$plugin_name}. Result: " . var_export( $result, true ) );
		}
	}

	// It's crucial to return the $response or a WP_Error object.
	// If you don't want to alter the response, simply return it as is.
	return $response;
}

// Add the filter to WordPress, specifying the correct number of accepted arguments.
add_filter( 'upgrader_post_install', 'my_custom_upgrade_handler', 10, 3 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

uncanny-automator-pro/src/core/admin/installer/includes/class-upgrader.php:558

* @param bool  $response   Installation response.
		 * @param array $hook_extra Extra arguments passed to hooked filters.
		 * @param array $result     Installation result data.
		 *
		 * @since 2.8.0
		 *
		 */
		$res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );

		if ( is_wp_error( $res ) ) {
			$this->result = $res;

			return $res;
		}


Scroll to Top