Home › Forums › Front-end Issues › Custom Validation › Reply To: Custom Validation
I recently noticed this too – I think it’s related to using the basic uploader vs wp media gallery rather than frontend/backend. Before I go further, you can also control the allowed file types in the Field Group Editor for your image field, you don’t have to use a custom filter.
The value that is passed to the filter from the basic uploader is “C:\fakepath\image.jpg” (I have a Mac, so it’s really a fake path. Going through the Media Gallery it has already uploaded the file, then passes the attachment ID to the filter, so for example 123
.
If you do want to use your custom filter you would just need to check to see if the value is numeric or not, and if it is fetch the attachment (which should already have the file type).
if ( !empty( $value ) && is_numeric( $value ) ){
// https://codex.wordpress.org/Function_Reference/get_post_mime_type
$mime_type = get_post_mime_type( $value );
} else {
// use the filename
$fileinfo = wp_check_filetype( $value );
$mime_type = $fileinfo['type'];
}
if( !in_array( str_replace( 'image/', '', $mime_type ), array( 'jpeg', 'gif', 'png', 'bmp', 'tiff', 'jpg' ) ) ){
$valid = "Please upload a valid image file!";
}
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.