Make WordPress Core

Opened 9 years ago

Last modified 8 years ago

#41753 reopened enhancement

_wp_get_attachment_relative_path uses hardcoded uploads directory path

Reported by: nedga055 Owned by:
Priority: normal Milestone: Future Release
Component: Media Version: 4.7.4
Severity: normal Keywords: has-patch
Cc: Focuses:

Description

I'm using a theme that in turn uses the _wp_get_attachment_relative_path to help generate paths to uploaded images.

However, because my WordPress install changes the uploads directory path, the function returns the file system path for the attachment rather than then relative path within the uploads directory as expected, which ultimately breaks the intended functionality.

In _wp_get_attachment_relative_path, would it make sense to do something like the following instead of hard coding 'wp-content/uploads':

$uploads_dir = wp_upload_dir();
$uploads_basedir = $uploads_dir['basedir'];
if ( false !== strpos( $dirname, $uploads_basedir ) ) {
	// Get the directory name relative to the upload directory (back compat for pre-2.7 uploads)
	$dirname = substr( $dirname, strpos( $dirname, $uploads_basedir ) + strlen($uploads_basedir) );
	$dirname = ltrim( $dirname, '/' );
}

Attachments (2)

41753 (511 bytes ) - added by nikolastoqnow 9 years ago.
41753.patch (511 bytes ) - added by nikolastoqnow 9 years ago.
The correct patch file

Download all attachments as: .zip

Change History (7)

@nikolastoqnow
9 years ago

@nikolastoqnow
9 years ago

The correct patch file

#1 @nikolastoqnow
9 years ago

  • Keywords has-patch added
  • Resolutioninvalid
  • Status newclosed

#2 @nikolastoqnow
9 years ago

  • Resolution invalid
  • Status closedreopened

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


8 years ago

#4 @antpb
8 years ago

This ticket was discussed in the recent media component meeting. We are wondering if this will impact multisite and would caution that we do some testing with it in that configuration. My initial digging found that we do account for multisite in the wp_upload_dir docs.

https://developer.wordpress.org/reference/functions/wp_upload_dir/

This below comment mentions wu_upload_dir(); accounts for multisite by appending sitename or id.

https://developer.wordpress.org/reference/functions/wp_upload_dir/#comment-2703

#5 @antpb
8 years ago

  • Milestone Awaiting ReviewFuture Release
Note: See TracTickets for help on using tickets.