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


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: