Created
September 5, 2024 04:12
-
-
Save gh640/3df4ae70e37445d629f202e4bc94f559 to your computer and use it in GitHub Desktop.
WordPress: Use user ids as subfolders for uploaded media files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Japanese post: