Support

Account

Home Forums General Issues acf/upload_prefilter – for every field on options page Reply To: acf/upload_prefilter – for every field on options page

  • Hi John,

    Thanks for your input. Using your method for logging the $_POST data, this is the result:

    Array
    (
        [name] => test.png
        [action] => upload-attachment
        [_wpnonce] => dee97d17e2
        [_acfuploader] => field_5a856f0eb49d7
    )

    So, there’s nothing being sent about the origin page, just the ACF field’s key.

    I guess the only and easiest way is just to create an array of all the fields (potentially only about 10, not that many in my use case). This is how I’ve solved it:

    $fields = array('field_1', 'field_2', 'field_3');
    
    foreach ($fields as $field) {
        add_filter("acf/upload_prefilter/name=$field", function($errors) {
            add_filter('upload_dir', function($uploads) {
                $dir = '/options';
                $uploads['url'] = $uploads['baseurl'] . $dir;
                $uploads['path'] = $uploads['basedir'] . $dir;
                return $uploads;
            });
            return $errors;
        });
    }