There is a code snippet on the GitHub titled Change the maximum number of images per listing #hivepress#listings
We’ve implemented it and set the max to 5 images, however when adding a listing it allows a the user to upload a max of 10 images and then it outputs a slightly incorrectly worded error of “Only up to 10 files can be uploaded.”. Then when saving the changes it displays the custom error of “A maximum of five images allowed.” as defined in the snippet and the user needs to delete 5 images.
Is there by chance another snippet that can be added to prevent the uploading of the 10 images or a modification to the current snippet?
And then while at it, a snippet that can change the default errors wording? I know there are possibly different forms but if you could possibly supply us with a base snippet and then we can modify it as required for different forms please.
Please send more details that may help to detect or reproduce this issue (e.g. the code snippets you use, screenshots, the steps to reproduce this issue, or the error message you get). It was tested locally and seems to be ok
When adding a listing the user selects 12 images and gets two errors for the 11’th and 12’th image they try to upload.
Error: Only up to 10 files can be uploaded.
Screenshot: Screenshot, 2023-04-14 13:21:05 - Paste.Pics
When then clicking the Submit listing button they get and additional error which is defined in the code snippet.
Error: A maximum of 5 images allowed.
Screenshot: Screenshot, 2023-04-14 13:23:21 - Paste.Pics
Code snippet:
add_filter(
'hivepress/v1/forms/listing_update/errors',
function( $errors, $form ) {
$listing = $form->get_model();
if ( $listing && count($listing->get_images__id()) > 5 ) {
$errors[] = 'A maximum of 5 images allowed.';
}
return $errors;
},
100,
2
);
So ideally we’d like to prevent the first step of being allowed to upload 10 images.
I assume the code snippet only gets executed when clicking the Submit Listing button.
Yes, currently this may be inconsistent because there are asynchronous uploads, e.g. 3 images may be uploaded simultaneously and there’s no reliable way to check the current number of images during this process yet. We’ll try to resolve this to ensure a strict image limit.