Filter Since 4.4.0 The $hook_extra parameter became available. uncanny-continuing-education-credits

upgrader_source_selection

Filter the source file location for the upgrade package. Filters the location of the upgrade package source before it is selected by the WordPress upgrader.

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

Description

Filters the temporary directory where an upgrade package is extracted. Developers can modify the `$source` path to point to an alternative location, useful for custom upgrade processes or complex file management. This hook fires after the upgrade package is downloaded but before it's processed further.


Usage

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

Parameters

$source (string)
File source location.
$remote_source (string)
Remote file source location.
$this (WP_Upgrader)
WP_Upgrader instance.
$hook_extra (array)
Extra arguments passed to hooked filters.

Return Value

The filtered value.


Examples

/**
 * Filter the source selection during the WordPress upgrade process.
 *
 * This example prioritizes downloading from a specific remote source
 * if it's available and deemed stable, otherwise it falls back to the
 * default selection. It's useful for forcing upgrades from a trusted
 * CDN or a staging environment before they hit the public repository.
 */
add_filter(
	'upgrader_source_selection',
	function ( $source, $remote_source, $upgrader_instance, $hook_extra ) {
		// Define your trusted source URL.
		$trusted_source_url = 'https://my-custom-cdn.com/wordpress-updates/';

		// Check if the remote source is from our trusted URL and if it's a valid URL.
		if ( strpos( $remote_source, $trusted_source_url ) === 0 && filter_var( $remote_source, FILTER_VALIDATE_URL ) ) {

			// In a real-world scenario, you'd add more robust checks here:
			// - Check for a specific file structure or manifest.
			// - Verify digital signatures or checksums for integrity.
			// - Potentially check for a 'stable' flag within the remote source.

			// For this example, we'll assume if it matches our trusted URL,
			// we want to use it.
			return $remote_source;
		}

		// If it's not our trusted source, return the original source selection.
		return $source;
	},
	10, // Priority: Lower numbers execute earlier.
	4   // Accepted args: Number of arguments the callback function accepts.
);

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

* @param WP_Upgrader $this          WP_Upgrader instance.
		 * @param array       $hook_extra    Extra arguments passed to hooked filters.
		 *
		 * @since 2.8.0
		 * @since 4.4.0 The $hook_extra parameter became available.
		 *
		 */
		$source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this, $args['hook_extra'] );

		if ( is_wp_error( $source ) ) {
			return $source;
		}

		// Has the source location changed? If so, we need a new source_files list.
		if ( $source !== $remote_source ) {

Scroll to Top