Media
Image, File, Gallery (and the Featured image target) store attachment IDs. The Media library item by ID, URL or file path transformation finds them:
| The source holds | Found by |
|---|---|
55 |
The attachment with ID 55 (checked to exist) |
https://example.com/wp-content/uploads/2024/05/photo.jpg |
The attachment whose file is 2024/05/photo.jpg |
https://…/photo-300x200.jpg |
The original photo.jpg of that resized copy |
2024/05/photo.jpg or /2024/05/photo.jpg |
The same, relative to the uploads folder |
| A list (gallery) | Each item, order kept |
All values of a batch are found with at most two queries. Nothing is found for URLs of other sites or paths outside the uploads folder.
Files that are not in the media library yet
The Connector never downloads files by itself: fetching arbitrary URLs from a migration file would let a file make your server request anything. Two safe ways:
- Copy the old uploads into
wp-content/uploadsand register them (for examplewp media importin WP-CLI), then use the lookup above. - Write a transformation that downloads from hosts you allow:
use Cybo\AdvancedFieldEngine\Connector\Transform\TransformFailed;
add_action( 'cafe_register_migration_transformers', function () {
cafe_register_migration_transformer(
'download_old_site_image',
'Download from old.example.com',
function ( $url, array $options, $context ) {
if ( ! $url || 'old.example.com' !== wp_parse_url( $url, PHP_URL_HOST ) ) {
throw new TransformFailed( 'Only images of old.example.com are downloaded.' );
}
if ( $context->dry_run ) {
return null; // Never download while checking.
}
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$id = media_sideload_image( esc_url_raw( $url ), 0, null, 'id' );
if ( is_wp_error( $id ) ) {
throw new TransformFailed( $id->get_error_message() );
}
return $id;
}
);
} );
Downloaded files are not removed by a rollback.
Checks
CAFE's field types check the attachment on save: it must exist, and its MIME type must be allowed by the field (Image: images; File: the field's allowed types). A missing or wrong attachment makes the record invalid, with the field and the reason in the report.