WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#39768 new defect (bug)

Incorrect image returned with attachment_url_to_postid()

Reported by: ben.greeley Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.2
Component: General Keywords:
Focuses: Cc:
PR Number:

Description

When two files are uploaded to the media library during the same month that have the same name and differ in capitalization, the function

attachment_url_to_postid()

can return the incorrect image.

For example, if two images were uploaded named untitled.jpg and Untitled.jpg, the SQL query that is being run in

attachment_url_to_postid()

isn't case-sensitive so it will use the first image that is returned, which may not be correct if the desired image is Untitled.jpg, but the image information that is returned is for untitled.jpg. The meta_value for each _wp_attached_file is saved as 2017/02/untitled.jpg and 2017/02/Untitled-1.jpg, so a query to match http://url.dev/wp-content/uploads/sites/9/2017/02/Untitled.jpg returns 2017/02/untitled.jpg, which is the incorrect image.

I created a suggested workaround, which grabs matches in case there was a duplicate filename and returns the closest case-sensitive result. https://gist.github.com/bengreeley/29a6edaca5c370091dd2cafc3369e400

Change History (3)

#1 @tristangemus
3 years ago

Welcome to core @ben.greeley

From what I can see Wordpress is handling this correctly. If a duplicate is found using the same case or not, that image is appended with 1, 2, and so on. If you uploaded an image as untitled.jpg twice, then the second time around the URL you need to pass should have the be appended with -1.

What's the use case where you're passing the original image URL even though Wordpress changes it before uploading?

#2 @ben.greeley
3 years ago

@tristangemus thanks for the prompt response. The use case is one image was named Unknown.jpg that was uploaded to a site. Later, unknown.jpg was added to the media library, specifically these two images: http://www.lamag.com/wp-content/uploads/sites/9/2017/01/Unknown.jpg and http://www.lamag.com/wp-content/uploads/sites/9/2017/01/unknown.jpg . When the full image is embedded onto a post, we have a link like <img class="aligncenter wp-image-671724 size-full" src="http://www.lamag.com/wp-content/uploads/sites/9/2017/01/unknown.jpg" />

There is some code that runs attachment_url_to_postid( 'http://www.lamag.com/wp-content/uploads/sites/9/2017/01/unknown.jpg' ), but the image that it returns is a reference to http://www.lamag.com/wp-content/uploads/sites/9/2017/01/Unknown.jpg . It appears this is happening because the table already has one value of 2017/01/Unknown.jpg, which matches its SQL query trying to find 2017/01/unknown.jpg since mySQL queries aren't case-sensitive.

I'm able to reproduce in my local environment.

#3 @tristangemus
3 years ago

Right but if you use Wordpress to insert the image, it should be referencing the image as Unknown-1.jpg. The only way I see this being an issue is if the image is manually being typed into the editor/codebase. In that case, you will get the incorrect image, but Wordpress is selecting the proper one based on intended logic.

If you are able to take a quick screen recording and upload it to Youtube, that would be helpful so we can see if this is a bug or not.

Thanks!

Note: See TracTickets for help on using tickets.