You can do something like this to create your own plugin upload folder in /wp-content/uploads
the following code will basically create a custom folder inside uploads called myplugin_uploads
and put an empty index.html
inside it.
Dynamically creating your custom upload folder
add_action( 'init', 'create_files' );
function create_files() {
$upload_dir = wp_upload_dir();
$files = array(
array(
'base' => $upload_dir['basedir'] . '/myplugin_uploads',
'file' => 'index.html',
'content' => ''
)
);
foreach ( $files as $file ) {
if ( wp_mkdir_p( $file['base'] ) && ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) {
if ( $file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'w' ) ) {
fwrite( $file_handle, $file['content'] );
fclose( $file_handle );
}
}
}
}
Now practically, you’ll need to change the upload dir/destination. You can do so using the upload_dir filter hook.
Example
add_filter('upload_dir', 'myplugin_upload_dir');
function myplugin_upload_dir( $param ){
$mydir="/myplugin_uploads";
$param['path'] = $param['path'] . $mydir;
$param['url'] = $param['url'] . $mydir;
return $param;
}
When you’re done you can remove the filter with code:
remove_filter('upload_dir', 'myplugin_upload_dir');