Skip to content

Remapping once media import does not works as expected for media hosted externally without file extension #186

@hbhalodia

Description

@hbhalodia

Issue

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.

$parts = pathinfo( $url );
$name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2
$parts_new = pathinfo( $upload['url'] );
$name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" );
$this->url_remap[ $parts['dirname'] . '/' . $name ] = $parts_new['dirname'] . '/' . $name_new;

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.

MediaImportExternalIssue.mov

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions