You can use the em_event_validate filter.
add_filter( 'em_event_validate', 'my_em_event_validate', 10, 2);
function my_em_event_validate( $is_valid, $EM_Event ) {
if ( $EM_Event->get_image_url() == '' ) {
$is_valid = false;
$EM_Event->add_error(__('You must provide a featured image.', 'events-manager'));
}
return $is_valid;
}
Thread Starter
enkoes
(@enkoes)
Thanks for your help.
I tested but the filter doesn’t work. Even though image is uploaded, the form submission is blocked by displaying the error message “You must provide a featured image.” I guess the image hasn’t been saved or attached as a featured image by the time validation runs.
Use the following code snippet instead to make the image upload a required field:
add_action('em_uploads_uploader_init', function() {
EM\Uploads\Uploader::$default_options['required'] = true;
} );
Thread Starter
enkoes
(@enkoes)
Hi did a brief test after activated your code snippet. When no image is uploaded, the form can still be submitted without validation.
Interesting, it worked for me. Did you enable the visual uploader?
Thread Starter
enkoes
(@enkoes)
Yes. I tested both (with visual uploader enable & disable), but didn’t help. User still can submit event without the image.
Well, like I said, it worked for me. Maybe someone else will be able to help you with this.
Thread Starter
enkoes
(@enkoes)
After spending some time into it, still can’t figure out why your code doesn’t work for me.
As an alternative, I use the following JS to make the image upload field required successfully. In case someone needs this:
document.addEventListener('DOMContentLoaded', function() {
var fileInput = document.querySelector('input[type="file"][name="event_image"]');
if (fileInput) {
fileInput.required = true;
}
});