acf/upload_prefilter

Overview

This filter allows you to run custom validation on attachments before they are uploaded. The image, file and gallery field include settings to validate width, height, size and type for uploads, however you may find it necessary to add customized logic for all or specific upload fields.

Changelog

  • Added in v5.1.9

Parameters

Name Type Description
$errors array An array of errors for the given file about to be uploaded. Appending to this array will result in the attachment failing to upload, and the error message displayed to the client.
$file array An array containing the $_FILE data for the attachment about to be uploaded
$field array An array containing all the field settings for the field which was used to upload the attachment

Usage

There are 4 ways to hook into acf/upload_prefilter filter.

  1. acf/upload_prefilter – filter for every field
  2. acf/upload_prefilter/type={$field_type} – filter for a specific field based on it’s type
  3. acf/upload_prefilter/name={$field_name} – filter for a specific field based on it’s name
  4. acf/upload_prefilter/key={$field_key} – filter for a specific field based on it’s key

functions.php

<?php

function my_acf_upload_prefilter( $errors, $file, $field ) {
    
    // only allow admin
    if( !current_user_can('manage_options') ) {
        
        $errors[] = 'Only administrators may upload attachments';
        
    }
    
    
    // return
    return $errors;
    
}

add_filter('acf/upload_prefilter', 'my_acf_upload_prefilter');

?>