get array of files in uploads dir

You should also check if that dir exists and loop through the list_files if they exist.

Since you asked for another idea to get those files: You can use the php function glob. It finds pathnames matching a pattern. You can find the Glob PHP manual here.


$current_user = wp_get_current_user();
$upload_dir   = wp_upload_dir();

$user_dirname = $upload_dir['basedir']."https://wordpress.stackexchange.com/".$current_user->user_login;
if ( ! file_exists( $user_dirname ) ) {
    wp_mkdir_p( $user_dirname );
    return;
}

foreach (glob($user_dirname  . "/*.mp3") as $filename) {
    echo $filename;
    echo do_shortcode('');
}