Filter
Since 4.4.0 The $hook_extra parameter became available.
uncanny-automator-pro
upgrader_source_selection
Filter the source file location for the upgrade package. Filters the upgrade package source location before the download and installation process begins.
add_filter( 'upgrader_source_selection', $callback, 10, 4 );
Description
Filters the selected upgrade source location. Developers can modify the $source string to point to a different directory or to return a WP_Error for custom upgrade handling. This hook allows for dynamic source selection before the upgrade package is extracted.
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
<?php
/**
* Conditionally modify the source selection for plugin upgrades.
*
* This example demonstrates how to intercept the upgrader_source_selection filter
* to potentially change the source directory where a plugin is unpacked.
* For instance, you might want to redirect upgrades to a specific staging environment
* or perform custom validation on the source.
*
* @param string $source The file source location.
* @param string $remote_source The remote file source location.
* @param WP_Upgrader $upgrader The WP_Upgrader instance.
* @param array $hook_extra Extra arguments passed to hooked filters.
* @return string|WP_Error The modified source location or a WP_Error object.
*/
function my_custom_upgrader_source_selection( $source, $remote_source, $upgrader, $hook_extra ) {
// Only apply this logic if we are upgrading a specific plugin.
// In a real-world scenario, you'd have a more robust way to identify
// the plugin, perhaps by checking $hook_extra['plugin'] or $upgrader->skin->plugin_info.
if ( isset( $hook_extra['plugin'] ) && 'my-special-plugin/my-special-plugin.php' === $hook_extra['plugin'] ) {
// Example: Redirect to a specific development directory if an environment variable is set.
if ( getenv( 'WP_DEV_ENV' ) === 'staging' && file_exists( '/path/to/staging/plugins/my-special-plugin/' ) ) {
return '/path/to/staging/plugins/my-special-plugin/';
}
// Example: Prevent upgrades from certain remote sources (e.g., a deprecated CDN).
if ( strpos( $remote_source, 'deprecated-cdn.example.com' ) !== false ) {
return new WP_Error(
'invalid_source',
__( 'Upgrades from this source are no longer permitted. Please use the official WordPress.org repository.', 'my-text-domain' )
);
}
// Example: Add a custom directory prefix if the source is from a specific reseller.
if ( strpos( $remote_source, 'reseller-a.example.com' ) !== false ) {
$custom_source = trailingslashit( $source ) . 'reseller-a-custom-files/';
if ( file_exists( $custom_source ) ) {
return $custom_source;
}
}
}
// Return the original source if no modifications were made.
return $source;
}
add_filter( 'upgrader_source_selection', 'my_custom_upgrader_source_selection', 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:458
* @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 ) {