acf/validate_attachment


目录

Description

Used to perform custom validation on an attachment and prevent it being uploaded or selected for a specific field.

Similar to the acf/upload_prefilter filter, this filter can be used to prevent an upload, however, it can also be used to prevent an attachment being selected from a WP media modal.

Changelog

Parameters

apply_filters( 'acf/validate_attachment', $errors, $file, $attachment, $field, $context );

Modifers

This filter provides modifiers to target specific fields. The following filter names are available:

Example

This example demonstrates how to validate the file name of all attachments. If the attachment’s file name does not begin with the string "acf-", an error message will be displayed causing the upload/selection to fail.

functions.php

<?php
add_filter('acf/validate_attachment', 'my_acf_validate_attachment', 10, 5);
function my_acf_validate_attachment( $errors, $file, $attachment, $field, $context ){

    // Check first 4 characters of the attachment name.
    if( substr($attachment['name'], 0, 4) !== 'acf-' ) {
        $errors[] = __( 'File name must begin with "acf-"' );
    }
    return $errors; 
}

An error message displayed in the WP media modal preventing an attachment from being selected.

An error message displayed in the WP media modal preventing an attachment from being selected.


版本号 #1
由 壳壳虫 创建于 2026-08-29 15:44:03 UTC
由 壳壳虫 更新于 2026-08-29 15:44:03 UTC