Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#53181 closed defect (bug) (invalid)

attachment_url_to_postid not working in latest wordpress

Reported by: hedgehog90's profile hedgehog90 Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.7.1
Component: Media Keywords: reporter-feedback
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Given the url of an existing attachment:
"https://localhost/wp-content/uploads/2021/05/14893-tiny-toon-adventures-nes-front-cover.jpg"

On wp-include/media.php line 4809 it does something strange:

if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) {
  $path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
}

It removes the baseurl, leaving a partial path that will never be found in the database.
However if instead it replaced $dir['baseurl'] with $dir["basedir"], it would create a valid $path.

Change History (4)

#1 in reply to: ↑ description @hedgehog90
4 years ago

Sorry, I forgot to include what it did to my url:

"https://localhost/wp-content/uploads/2021/05/14893-tiny-toon-adventures-nes-front-cover.jpg"
becomes:
"2021/05/14893-tiny-toon-adventures-nes-front-cover.jpg"
before it is processed by the query, which makes no sense.

#2 @SergeyBiryukov
4 years ago

  • Component changed from General to Media
  • Description modified (diff)
  • Keywords reporter-feedback added

Hi there, welcome back to WordPress Trac! Thanks for the ticket.

Just noting that I could not reproduce the issue on a clean install, attachment_url_to_postid() seems to work as expected for me.

On wp-include/media.php line 4809 it does something strange:

if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) {
  $path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
}

It removes the baseurl, leaving a partial path that will never be found in the database.

This particular code was added in [30501] / #30346 to replace incorrect ltrim() usage introduced with the function in [29029] / #27891.

This matches how the _wp_attached_file post meta field is stored in the database, it is supposed to be a path relative to the uploads directory and not a full URL nor a full path from the root directory. See [8796] / #7622 and [36120] / #35106 for more context.

However, if the site was upgraded from an older install (before WordPress 2.7), it is possible that some _wp_attached_file values contain a full URL, which indeed might be something that attachment_url_to_postid() does not account for.

Could you share the _wp_attached_file meta value for the file in question?

#3 @hedgehog90
4 years ago

  • Resolution set to invalid
  • Status changed from new to closed

My DB appears to be the issue then.
For some reason all my _wp_attached_file values are absolute file paths, think I know what plugin to blame...

Thanks for your help!

#4 @peterwilsoncc
4 years ago

  • Milestone Awaiting Review deleted

Thanks for following up, it's very much appreciated.

Note: See TracTickets for help on using tickets.