[wp-trac] [WordPress Trac] #41816: attachment_url_to_postid() should find post_id for URLs of intermediate size images
WordPress Trac
noreply at wordpress.org
Wed Sep 6 17:11:32 UTC 2017
#41816: attachment_url_to_postid() should find post_id for URLs of intermediate
size images
-------------------------------------------------+-------------------------
Reporter: pbiron | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting
Component: Media | Review
Severity: normal | Version: trunk
Keywords: has-patch dev-feedback 2nd-opinion | Resolution:
has-unit-tests | Focuses:
-------------------------------------------------+-------------------------
Comment (by pbiron):
Yet another thing I'd appreciate feedback on is the following.
Suppose the `thumbnail` size is `150x150`, an image is uploaded and the
thumbnail sized image is added to a post, e.g., the post content looks
like:
{{{
blah, blah, blah
<img src='http://example.com/wp-content/uploads/test-150x150.jpg' />
yada, yada, yada
}}}
Then at a later time, the `thumbnail` size is changed to `100x100` and
attachment metadata is updated for all existing images. Even tho the
image in that post will still display correctly,
{{{
$attachment_id = attachment_url_to_postid( 'http://example.com/wp-
content/uploads/test-150x150.jpg' );
}}}
will not find it's post ID (since `_wp_attachment_metadata['sizes']` will
no longer contain `test-150x150.jpg`) and it would be good if it could.
The only ways I can think to solve this particular problem are very
computationally expensive and/or prone to false positives. Can anyone
think an air-tight solution that is efficient?
**Note**
This particular question arises from my attempt to fix what I consider a
bug in the exporter (see [[https://github.com/pbiron/wordpress-
exporter-v2/issues/3|Exporter Redux: Issue 3]]). An export with
`post_type = post` will **not** contain the attachment and, hence, when
imported into another site the image in that post will still point to the
exporting site instead of being rewritten to point to a newly imported
attachment.
To address that exporter bug, I've written code that extracts attachment
URLs from posts being exported, gets the attachment post ID using
`attachment_url_to_postid()` (actually, until the patch in this ticket is
accepted/merged, it uses a differently named function whose implementation
is the same as in the patch) and adds those attachment posts to the
export, but because of this problem that code is missing attachments such
as the above.
Granted, even if this problem is solved, there is still a chance the
intermediate size in question is not available on the importing site, and
hence, the image in the imported post wouldn't display. But, //that//
particular problem is relatively easy to fix by having the exporter add
"metadata" to the WXR file for such intermediate image sizes which the
importer could then add (using `add_image_size()`) temporarily during the
import.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/41816#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list