[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 Feb 20 22:09:02 UTC 2019
#41816: attachment_url_to_postid() should find post_id for URLs of intermediate
size images
-------------------------------------------------+-------------------------
Reporter: pbiron | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting
| Review
Component: Media | Version: 4.9
Severity: normal | Resolution:
Keywords: has-patch dev-feedback 2nd-opinion | Focuses:
has-unit-tests |
-------------------------------------------------+-------------------------
Comment (by leodandesign):
I've implemented as very simple solution that use preg_replace to remove
the dimension element of the url and then run the
attachment_url_to_postid() function.
{{{#!php
<?php
$image = preg_replace('/-\d+[Xx]\d+\./', '.', $thumbnail );
$image_id = attachment_url_to_postid($image);
}}}
It's not elegant, but effective. Couldn't this be dropped into
attachment_url_to_postid() function to pull get the url using a simple
pattern instead of making another database lookup.
In the real world, I am using this to pull through a full size image for a
Schema Json-LD object.
I've also used it in the past as part of function to filter the img src to
replace it with the smaller img of the same ratios, for a front end
javascript function to dynamically calculate the correct srcset img to get
based on image width on-screen.
Again, not elegant, but works.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/41816#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list