Make WordPress Core

Opened 14 months ago

Last modified 4 weeks ago

#51058 new defect (bug)

attachment_url_to_postid does not retrieve post ID of really large images

Reported by: littler.chicken Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3
Component: Media Keywords: has-patch needs-testing
Focuses: Cc:


Since WordPress now sets the _wp_attached_file meta value for very large images to use the scaled image:




any function which uses attachment_url_to_postid on a large image URL will not properly retrieve the image's post ID.

Use case: I use this function after removing intermediate image size strings to get the post ID of the uploaded image. I'm currently getting around this issue by inserting -scaled into the image URL and running the function again if a post ID is not returned, but I would prefer if WP could account for this, please.

Alternatively, if the WordPress function could manage the intermediate size conversion on its own, that would also resolve the issue by removing the need to remove it in the first place.

Attachments (1)

attachment_url_to_postid.patch (2.7 KB) - added by jdorner 4 weeks ago.
Patch to address making attachment_url_to_postid work for all attachment sizes and for permalinks and for "scaled" images

Download all attachments as: .zip

Change History (7)

#1 @Mista-Flo
14 months ago

Can you give steps to reproduce please? And any screenshots if it can help.

#2 @littler.chicken
14 months ago

To reproduce, upload a very large image to your site (wider than 2560px). Get the URL of the original uploaded image.

Add code to dump out some information similar to this:

	function( $content ) {
		// Enter the URL of the original image
		var_dump( attachment_url_to_postid( 'https://nightly.local/wp-content/uploads/2020/03/roman-kraft-197672.jpg' ) );
		// Now add -scaled to the original image name
		var_dump( attachment_url_to_postid( 'https://nightly.local/wp-content/uploads/2020/03/roman-kraft-197672-scaled.jpg' ) );
		return $content;

For the first, WordPress will output 0 as no image exists with that filename. The second will return the image ID, because that's the filename assigned to the _wp_attached_file meta key (ref: https://core.trac.wordpress.org/browser/tags/5.5/src/wp-includes/media.php#L4634).

When I use this function, it's in the context of parsing the RSS feed, at which point the original ID is not available, but I'm retrieving it from the database so that the image can be replaced with a smaller copy. Currently, without a secondary check for the -scaled image, the replacement fails for very large images.

#3 @metalandcoffee
12 months ago

  • Version changed from trunk to 5.3

I was able to reproduce this issue.

Appending -scaled to an oversized PNG images was introduced in 5.3 (https://core.trac.wordpress.org/ticket/48736).

The function attachment_url_to_postid is simply doing a check against the database with whichever url you give it:

$sql = $wpdb->prepare(
"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path );

So it makes sense that it'll return nothing since the file was immediately renamed upon upload.

#4 @celloexpressions
8 months ago

This should be fixed similarly to #41816.

4 weeks ago

Patch to address making attachment_url_to_postid work for all attachment sizes and for permalinks and for "scaled" images

#5 @jdorner
4 weeks ago

  • Keywords has-patch needs-testing added
Note: See TracTickets for help on using tickets.