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 of the process.
add_filter( 'upgrader_pre_install', $callback, 10, 2 );
Description
Fires before a plugin or theme installation begins. Developers can return a `WP_Error` object or a truthy value to prevent the installation, effectively short-circuiting the process with a custom response. This hook offers an early interception point for managing installation workflows.
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 a specific plugin during the upgrade process.
*
* This example hook checks if the plugin being installed is 'bad-plugin/bad-plugin.php'
* and if so, it returns a WP_Error to halt the installation.
*
* @param bool|WP_Error $response The response from the filter. Defaults to true.
* @param array $hook_extra Extra arguments passed to hooked filters.
* Contains 'plugin' (slug/file) and 'type' (plugin/theme).
* @return bool|WP_Error Returns a WP_Error if the plugin should be blocked,
* otherwise returns the original $response.
*/
function my_prevent_bad_plugin_upgrade( $response, $hook_extra ) {
// Ensure we are dealing with a plugin upgrade and have the necessary data.
if ( ! isset( $hook_extra['type'] ) || 'plugin' !== $hook_extra['type'] || ! isset( $hook_extra['plugin'] ) ) {
return $response; // Not a plugin upgrade or missing data, pass through.
}
$plugin_slug_file = $hook_extra['plugin']; // e.g., 'woocommerce/woocommerce.php'
// Define the plugin we want to block from upgrading.
$plugin_to_block = 'bad-plugin/bad-plugin.php';
if ( $plugin_slug_file === $plugin_to_block ) {
// Return a WP_Error to stop the installation process.
return new WP_Error(
'upgrade_error',
sprintf(
__( 'The plugin "%s" is blocked from automatic upgrades. Please contact support.', 'your-text-domain' ),
'Bad Plugin' // You might want to fetch the actual plugin name if possible.
)
);
}
// If the plugin is not the one we want to block, return the original response.
return $response;
}
add_filter( 'upgrader_pre_install', 'my_prevent_bad_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
src/install-automator/vendor/uncanny-one-click-installer/includes/PluginSilentUpgrader.php:424
*
* @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'];