I collaborate with the development of the theme Odin Framework, which has several classes that leverage the launch of a site from scratch. Among them, we have the abstract-front-end-form.php (link to function line), which prepares form fields for the front end, and class-contact-form.php to create a simple and effective contact form.
My friends have adopted the media_handle_upload function for the file type fields. The problem is that by default it is mandatory and we would like to have it present in the form but optional. But by submitting the form with the empty 'file' field it returns the error 'No file was uploaded.' Which is a native validation of the media_handle_upload function.
One way I found to skip this validation was by conditioning the error string.
if ( is_wp_error( $attachment_id ) && $attachment_id->get_error_message() !== 'No file was uploaded.' ) ) {
$error = apply_filters( 'odin_front_end_form_upload_error_' . $this->id, sprintf( '%s %s.', '<strong>' . $this->get_field_label( $id ) . '</strong>', $attachment_id->get_error_message() ) );
$this->set_errors( $error );
} else {
...
}
So it lets me submit the form with the empty file
field (optional).
We may well go with this solution, but it seems a bit obscure, since in a WP core update these upload error strings can be modified.
If anyone knows otherwise that I do not need to use a plugin or features, I will be grateful for their thoughts.