admin管理员组

文章数量:1391746

I am aware what I'm attempting to do is not recommended - please don't reply just to tell me this.

I have set the uploads directory to root by adding define( 'UPLOADS', '' ); to wp-config.php, which works fine except the URLs for my uploads end up with a double slash (//photo.jpg). The address I've defined for my website in the General settings does not have a trailing slash.

While this doesn't break anything, it looks a little peculiar in the event anyone copies the URL, so it would be preferable to resolve. So far I've tried using an absolute URL as per this answer (doing so just results in ://example/photo.jpg) and inserting a backspace character as per this answer, which was a bit of a longshot.

I am aware what I'm attempting to do is not recommended - please don't reply just to tell me this.

I have set the uploads directory to root by adding define( 'UPLOADS', '' ); to wp-config.php, which works fine except the URLs for my uploads end up with a double slash (https://example//photo.jpg). The address I've defined for my website in the General settings does not have a trailing slash.

While this doesn't break anything, it looks a little peculiar in the event anyone copies the URL, so it would be preferable to resolve. So far I've tried using an absolute URL as per this answer (doing so just results in https://example/https://example/photo.jpg) and inserting a backspace character as per this answer, which was a bit of a longshot.

Share Improve this question asked Feb 9, 2020 at 8:53 spacer GIFspacer GIF 1183 bronze badges 4
  • So did you turn off the "Organize my uploads into month- and year-based folders" setting? – Sally CJ Commented Feb 9, 2020 at 15:22
  • @SallyCJ Yes indeed. – spacer GIF Commented Feb 9, 2020 at 18:24
  • In that case, the answer might help you. Let me know. :) – Sally CJ Commented Feb 10, 2020 at 0:42
  • When understanding well you have the upload folder moved directly in WP root folder? If so and just as example calling that folder storage, try adding following in wp-config.php code define( 'UPLOADS', ''.'storage' ); – Charles Commented Feb 10, 2020 at 15:21
Add a comment  | 

1 Answer 1

Reset to default 1

The WordPress uploads directory's path and URL are determined by the wp_upload_dir() function which applies a filter named upload_dir which you can use to fix the double slashes in the attachment URL.

So if you turned off the "Organize my uploads into month- and year-based folders" setting, try the following:

add_filter( 'upload_dir', function ( $data ) {
  $data['baseurl'] = untrailingslashit( $data['baseurl'] );
  return $data;
} );

And you can actually use the upload_dir filter to change the uploads directory path/URL/etc. without having to use the UPLOADS constant..

本文标签: Correctly using the root directory for media uploads