Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 5 years ago

#10559 closed enhancement (invalid)

Allow a custom subdir in wp_upload_dir

Reported by: aaroncampbell Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Upload Keywords: has-patch needs-testing
Focuses: Cc:


Sometimes it's nice to keep a certain set of uploaded files separate from the rest. Especially if you're not allowing direct access to them. I usually do this by putting the files in a subdirectory of the uploads directory with a .htaccess rule to block access. I've been using a modified version of the wp_upload_dir which allows you to specify a sub-directory. For example, wp_upload_dir( null, 'premiums') would use a directory like wp-content/uploads/premiums/2009/08

Attachments (2)

10559.001.diff (2.1 KB) - added by aaroncampbell 11 years ago.
10559.001.2.diff (2.3 KB) - added by aaroncampbell 11 years ago.

Download all attachments as: .zip

Change History (9)

#1 @Denis-de-Bernardy
11 years ago

I'd be disagreeable on that one. passing the post_id to the upload_dir filter would be a *much* better implementation. it's then the plugin's job to determine if any subdir applies.

#2 @Denis-de-Bernardy
11 years ago

  • Component changed from Filesystem to Upload
  • Owner dd32 deleted

#3 follow-up: @aaroncampbell
11 years ago

Not all uploads have a post.

Also, the second patch was just to add the documentation. I meant to have it overwrite the first upload...oops.

#4 @aaroncampbell
11 years ago

I'm sorry, I should be more specific. I'm using my custom wp_upload_dir function to upload files to the right place which aren't associated with any post. They are just for a plugin. For example, a plugin like blog icons by Joost could use this to put the icons in wp-content/uploads/blog-icons and my attachment manager plugin could upload the icons used for attachments to wp-content/uploads/attachment-manager. Those examples both assume that the blog is using the default wp-content/uploads, but if the blog has a different upload dir, that would be used (%%upload_dir%%/%%subdir%%/ or %%upload_dir%%/%%subdir%%/YYYY/MM/)

#5 in reply to: ↑ 3 @Denis-de-Bernardy
11 years ago

Replying to aaroncampbell:

Not all uploads have a post.

Also, the second patch was just to add the documentation. I meant to have it overwrite the first upload...oops.

in this case, you don't need the patch at all.

add_filter('upload_dir', 'foobar');
$dir = upload_dir()l
remove_filter('upload_dir', 'foobar');

and have foobar() amend the dir as needed. what's desperately needed in that function is a post_id argument; not a subdir argument.

#6 @aaroncampbell
11 years ago

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

Good point. I don't know why I missed the obvious. Here is what I did for anyone else ending up here with a similar issue:

add_filter('upload_dir', 'my_upload_dir');
$upload = wp_upload_dir();
remove_filter('upload_dir', 'my_upload_dir');

funcion my_upload_dir($upload) {
	$upload['subdir']	= '/sub-dir-to-use' . $upload['subdir'];
	$upload['path']		= $upload['basedir'] . $upload['subdir'];
	$upload['url']		= $upload['baseurl'] . $upload['subdir'];
	return $upload;

#7 @DrewAPicture
5 years ago

  • Milestone 2.9 deleted
Note: See TracTickets for help on using tickets.