WP_Upgrader::download_package() WordPress Method

The download_package() method is used to download a package from a given URL. It takes two arguments: the package URL and an optional array of arguments. The download_package() method is used to download a package from a given URL. It takes two arguments: the package URL and an optional array of arguments. The first argument is the URL of the package to download. The second argument is an optional array of arguments. These arguments include the filename of the package, the destination directory, and the unzipping method. If you are not using the default arguments, you must pass an array of arguments to the download_package() method. The arguments you can pass are: 'filename' => // The name of the file to download 'destination' => // The destination directory 'unzip' => // If true, unzip the file 'overwrite' => // If true, overwrite existing files The download_package() method will return a WP_Error object on failure or the path to the downloaded file on success.

WP_Upgrader::download_package( string $package, bool $check_signatures = false, array $hook_extra = array() ) #

Download a package.


Parameters

$package

(string)(Required)The URI of the package. If this is the full path to an existing local file, it will be returned untouched.

$check_signatures

(bool)(Optional)Whether to validate file signatures.

Default value: false

$hook_extra

(array)(Optional)Extra arguments to pass to the filter hooks.

Default value: array()


Top ↑

Return

(string|WP_Error) The full path to the downloaded package file, or a WP_Error object.


Top ↑

Source

File: wp-admin/includes/class-wp-upgrader.php

	public function download_package( $package, $check_signatures = false, $hook_extra = array() ) {
		/**
		 * Filters whether to return the package.
		 *
		 * @since 3.7.0
		 * @since 5.5.0 Added the `$hook_extra` parameter.
		 *
		 * @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.
		 */
		$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'] );
		}

		$this->skin->feedback( 'downloading_package', $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;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0Added the $hook_extra parameter.
5.2.0Added the $check_signatures parameter.
2.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.