Support

Account

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!";
    }