WordPress Upload Folder Subdirectories

I have created a number of subfolders within the WordPress uploads folder and would like to programmatically designate which folder a file should upload to based on the current page being shown on within the dashboard.

Here is the code I have thus far:

function field_name_upload_prefilter($errors) {
    // in this filter we add a WP filter that alters the upload path
    add_filter('upload_dir', 'field_name_upload_dir');
        return $errors;
}
// Second filter
function field_name_upload_dir($uploads) {
    // here is where we later the path
    if($pagenow == 'archive'){
        $uploads['path'] = $uploads['basedir'].'/archive';
        $uploads['url'] = $uploads['baseurl'].'/archive';
        $uploads['subdir'] = '';
    }

    if ($pagenow = 'assets') {
        $uploads['path'] = $uploads['basedir'].'/assets';
        $uploads['url'] = $uploads['baseurl'].'/assets';
        $uploads['subdir'] = '';
    }

    return $uploads;
}

Read more here: WordPress Upload Folder Subdirectories

Leave a Reply

Your email address will not be published. Required fields are marked *