upgrader_pre_install
Filter the install response before the installation has started. Returning a truthy value, or one that could be evaluated as a WP_Error will effectively short-circuit the installation, returning that value instead. Filters the install response before installation begins, allowing short-circuiting with a truthy value or WP_Error.
add_filter( 'upgrader_pre_install', $callback, 10, 2 );
Description
Allows developers to intercept and modify the installation process for plugins and themes. This filter fires just before installation begins, enabling custom logic to prevent installation or provide a specific `WP_Error` response, effectively short-circuiting the upgrade. Use this to implement custom pre-installation checks or conditional upgrades.
Usage
add_filter( 'upgrader_pre_install', 'your_function_name', 10, 2 );
Parameters
-
$response(bool|WP_Error) - Response.
-
$hook_extra(array) - Extra arguments passed to hooked filters.
Return Value
The filtered value.
Examples
<?php
/**
* Prevent installation of specific plugins during upgrades.
*
* This example demonstrates how to hook into 'upgrader_pre_install' to
* intercept the installation process. It checks if the plugin being
* installed is 'my-problematic-plugin/my-problematic-plugin.php'. If it is,
* it returns a WP_Error to halt the installation and logs a message.
*
* @param bool|WP_Error $response The current response. Defaults to true.
* @param array $hook_extra Extra arguments passed to hooked filters.
* Contains details about the upgrade.
* @return bool|WP_Error The modified response. If a WP_Error is returned,
* the upgrade process will be aborted.
*/
function my_prevent_problematic_plugin_upgrade( $response, $hook_extra ) {
// Check if the current upgrade is for a plugin and if it's our problematic one.
if ( isset( $hook_extra['plugin'] ) && 'my-problematic-plugin/my-problematic-plugin.php' === $hook_extra['plugin'] ) {
// Log a message for debugging purposes.
error_log( 'Attempted to upgrade my-problematic-plugin. Installation aborted.' );
// Return a WP_Error to halt the upgrade process.
return new WP_Error(
'plugin_upgrade_blocked',
__( 'The upgrade for "My Problematic Plugin" is temporarily disabled due to a known issue. Please check our support forums for updates.', 'your-text-domain' )
);
}
// If it's not the problematic plugin, allow the upgrade to proceed.
return $response;
}
add_filter( 'upgrader_pre_install', 'my_prevent_problematic_plugin_upgrade', 10, 2 );
?>
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:423
*
* @param bool|WP_Error $response Response.
* @param array $hook_extra Extra arguments passed to hooked filters.
*
* @since 2.8.0
*
*/
$res = apply_filters( 'upgrader_pre_install', true, $args['hook_extra'] );
if ( is_wp_error( $res ) ) {
return $res;
}
// Retain the Original source and destinations
$remote_source = $args['source'];