Remapping once media import does not works as expected for media hosted externally without file extension
Issue
- For example, create a WXR file with minimum of requirements
wp_attachment_urlwith an external URL without file extension. - Then, create a new Google Doc add an image to the doc and use the static URL provided, it would be something like https://lh7-rt.googleusercontent.com/docsz/AD_4nXepU93bn7nmRtZwpqQOGO2PeIwurdNY1DCOl6WBIrPnYwLFOz1DfoLZD8hcHY2Es7LYQBUIxqB2aHl3o89Xlg2RWwnG2NLdA7NAYm57IhyzI6uvhIMZx4grzgUsCShkZb5VrqFV?key=KJq1w2dlYSfo8kldshcGURv7
XML file format,
<item>
<title><![CDATA[view-after-2]]></title>
<dc:creator><![CDATA[admin]]></dc:creator>
<wp:post_name><![CDATA[view-after-2]]></wp:post_name>
<wp:status><![CDATA[inherit]]></wp:status>
<wp:post_type><![CDATA[attachment]]></wp:post_type>
<wp:attachment_url><![CDATA[https://lh7-rt.googleusercontent.com/docsz/AD_4nXepU93bn7nmRtZwpqQOGO2PeIwurdNY1DCOl6WBIrPnYwLFOz1DfoLZD8hcHY2Es7LYQBUIxqB2aHl3o89Xlg2RWwnG2NLdA7NAYm57IhyzI6uvhIMZx4grzgUsCShkZb5VrqFV?key=KJq1w2dlYSfo8kldshcGURv7]]></wp:attachment_url>
</item>
Add this item to the WXR file for the post, and use the image block with source as the link given.
<item>
<title><![CDATA[Image with External URL]]></title>
<dc:creator><![CDATA[admin]]></dc:creator>
<content:encoded><![CDATA[<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXepU93bn7nmRtZwpqQOGO2PeIwurdNY1DCOl6WBIrPnYwLFOz1DfoLZD8hcHY2Es7LYQBUIxqB2aHl3o89Xlg2RWwnG2NLdA7NAYm57IhyzI6uvhIMZx4grzgUsCShkZb5VrqFV?key=KJq1w2dlYSfo8kldshcGURv7" alt=""/></figure>
<!-- /wp:image -->]]></content:encoded>
<wp:post_date><![CDATA[2025-03-24 11:08:36]]></wp:post_date>
<wp:post_date_gmt><![CDATA[2025-03-24 11:08:36]]></wp:post_date_gmt>
<wp:post_modified><![CDATA[2025-03-24 12:08:44]]></wp:post_modified>
<wp:post_modified_gmt><![CDATA[2025-03-24 12:08:44]]></wp:post_modified_gmt>
<wp:post_name><![CDATA[test-images-with-upload]]></wp:post_name>
<wp:status><![CDATA[publish]]></wp:status>
<wp:post_type><![CDATA[post]]></wp:post_type>
</item>
Combine both the items to one standard XML, WXR format and try to import the file.
You would notice that, the media has been imported correctly, post have been imported correctly, but when you open post in edit mode, the media would look broken because, the extension to the media is not present, hence $this->url_remap, mapped the URLs without extension.
https://github.com/WordPress/wordpress-importer/blob/2ed3a298df5ec341eee22aadb5e2ab74435b1da9/src/class-wp-import.php#L1042-L1048
The above source code is creating the issue, because the source URL does not have any extension, hence anytime this array uses the key, it replaces the old value to new value, and new value as per the given link is removing the file extension itself.
This is the only case with an external file without extension, So we should have a condition for checking if the source URL, wp_attachment_url does not contain extension, we should not run the above lines of code to make everything works as expected.
Here is the attached video, that would demonstrate the difference between external URL and WordPress specific URL media and post import with specific scenarios.
https://github.com/user-attachments/assets/f7d19c88-8b7c-4dd0-9be2-7ac1021536d5