Skip to content

Instantly share code, notes, and snippets.

@gh640
Created September 5, 2024 04:12
Show Gist options
  • Save gh640/3df4ae70e37445d629f202e4bc94f559 to your computer and use it in GitHub Desktop.
Save gh640/3df4ae70e37445d629f202e4bc94f559 to your computer and use it in GitHub Desktop.
WordPress: Use user ids as subfolders for uploaded media files
<?php
add_filter( 'upload_dir', 'themeslug_upload_dir' );
/**
* Use user ids as subfolders for uploaded media files.
*/
function themeslug_upload_dir( $uploads ) {
$user_id = get_current_user_id() ?: 'anonymous';
$subdir = "/users/${user_id}" . $uploads['subdir'];
$uploads['path'] = $uploads['basedir'] . $subdir;
$uploads['url'] = $uploads['baseurl'] . $subdir;
return $uploads;
}
@gh640
Copy link
Author

gh640 commented Sep 5, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment