Filter Since 5.5.0 Added the `$hook_extra` parameter. uncanny-automator-pro

upgrader_pre_download

Filters whether to return the package. Filters whether to return the package before the WordPress upgrade process begins.

add_filter( 'upgrader_pre_download', $callback, 10, 4 );

Description

Allows developers to intercept and modify the download process for WordPress updates. This filter determines whether to proceed with downloading a package or to bail out early. It's useful for custom validation, pre-download actions, or preventing specific updates. The filter receives the package URL, the upgrader instance, and extra hook arguments.


Usage

add_filter( 'upgrader_pre_download', 'your_function_name', 10, 4 );

Parameters

$reply (bool)
Whether to bail without returning the package. Default false.
$package (string)
The package file name.
$this (WP_Upgrader)
The WP_Upgrader instance.
$hook_extra (array)
Extra arguments passed to hooked filters.

Return Value

The filtered value.


Examples

/**
 * Prevent the download of a specific plugin during upgrades.
 *
 * This filter can be used to intercept plugin and theme update processes
 * and conditionally prevent the download of a package. For example,
 * you might want to block updates for a plugin that is known to cause
 * conflicts with your custom code.
 *
 * @param bool        $reply      Whether to bail without returning the package. Default false.
 * @param string      $package    The package file name.
 * @param WP_Upgrader $upgrader   The WP_Upgrader instance.
 * @param array       $hook_extra Extra arguments passed to hooked filters.
 *
 * @return mixed If true, the update process will stop and return early.
 *               If false, the update process will continue.
 */
function my_prevent_specific_plugin_download( $reply, $package, $upgrader, $hook_extra ) {
    // Check if the current upgrade is for a plugin.
    if ( isset( $hook_extra['plugin'] ) && 'my-problematic-plugin/my-problematic-plugin.php' === $hook_extra['plugin'] ) {
        // If it's the problematic plugin, return true to stop the download.
        // You could also return a WP_Error object for a more descriptive message.
        return true;
    }

    // If it's not the problematic plugin, allow the download to proceed.
    return $reply;
}
add_filter( 'upgrader_pre_download', 'my_prevent_specific_plugin_download', 10, 4 );

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:275

public function download_package( $package, $check_signatures = false, $hook_extra = array() ) {

		/**
		 * Filters whether to return the package.
		 *
		 * @param bool        $reply      Whether to bail without returning the package.
		 *                                Default false.
		 * @param string      $package    The package file name.
		 * @param WP_Upgrader $this       The WP_Upgrader instance.
		 * @param array       $hook_extra Extra arguments passed to hooked filters.
		 *
		 * @since 3.7.0
		 * @since 5.5.0 Added the `$hook_extra` parameter.
		 *
		 */
		$reply = apply_filters( 'upgrader_pre_download', false, $package, $this, $hook_extra );
		if ( false !== $reply ) {
			return $reply;
		}

		if ( ! preg_match( '!^(http|https|ftp)://!i', $package ) && file_exists( $package ) ) { // Local file or remote?
			return $package;
			// Must be a local file.
		}

		if ( empty( $package ) ) {
			return new WP_Error( 'no_package', $this->strings['no_package'] );
		}

		$download_file = download_url( $package, 300, $check_signatures );

		if ( is_wp_error( $download_file ) && ! $download_file->get_error_data( 'softfail-filename' ) ) {
			return new WP_Error( 'download_failed', $this->strings['download_failed'], $download_file->get_error_message() );
		}

		return $download_file;
	}

Scroll to Top