Home › Forums › Add-ons › Gallery Field › Server-Side Validation for Image and Gallery Fields › Reply To: Server-Side Validation for Image and Gallery Fields
Support got back to me very quickly! The fix is as follows:
/**
* Allow PDFs in ACF Gallery fields (ACF 6.8.7+).
*
* ACF 6.8.7 added server-side enforcement that restricts Image and Gallery
* fields to image files only. These three filters restore PDF support while
* leaving the image-only restriction in place for every other file type.
*
* To target a single gallery field instead of all gallery fields, replace
* /type=gallery with /key=field_XXXXXXXX in all three filter tags.
*/
add_filter( ‘acf/get_image_mime_types/type=gallery’, function( $mimes ) {
return $mimes + array( ‘mp4’ => ‘video/mp4’ );
} );add_filter( ‘acf/validate_is_image_attachment/type=gallery’, function( $errors, $file, $attachment ) {
$type = ! empty( $attachment[‘id’] )
? get_post_mime_type( $attachment[‘id’] )
: ( wp_check_filetype( $attachment[‘name’] ?? ” )[‘type’] ?? ( $attachment[‘mime’] ?? $attachment[‘type’] ?? ” ) );if ( ‘video/mp4’ === $type ) {
unset( $errors[‘invalid_image’] );
}return $errors;
}, 10, 3 );add_filter( ‘acf/validate_value/type=gallery’, function( $valid, $value, $field, $input ) {
if ( true === $valid || ! is_array( $value ) ) {
return $valid;
}foreach ( $value as $id ) {
if ( ! is_numeric( $id ) ) {
return $valid;
}if ( ! wp_attachment_is_image( $id ) && ‘video/mp4’ !== get_post_mime_type( $id ) ) {
return $valid;
}
}$min = (int) ( $field[‘min’] ?? 0 );
if ( $min && count( $value ) < $min ) {
return sprintf(
_n( ‘%1$s requires at least %2$s selection’, ‘%1$s requires at least %2$s selections’, $min, ‘acf’ ),
$field[‘label’],
$min
);
}return true;
}, 20, 4 );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.