Listing max images

Hi there HP team.

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 :wink: 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

Hi there Yevhen

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.

And just FYI, there are some other scripts running on the Add listing page as well as my cut/copy/past PHP skills don’t allow me to merge them all. :upside_down_face:

**Image required:**
*add_filter(*
*	'hivepress/v1/forms/listing_update/errors',*
*	function( $errors, $form ) {*
*		$listing = $form->get_model();*

*		if ( $listing && ! count($listing->get_images__id()) > 1 ) {*
*			$errors[] = 'Please upload at least one image for this listing.';*
*		}*

*		return $errors;*
*	},*
*	100,*
*	2*
*);*

*add_filter(*
*	'hivepress/v1/forms/listing_update',*
*	function( $form ) {*
*		$form['fields']['images']['statuses']['optional'] = null;*

*		return $form;*
*	},*
*	1000*
*);*

**Field Labels and Descriptions:**
*add_filter(*
*	'hivepress/v1/forms/listing_update',*
*	function( $form ) {*
*		*
*		if(isset($form['fields']['images'])){*
*		 $form['fields']['images']['description'] = '(1 image required, no more than 5)';*
*		}*
*		if(isset($form['fields']['title'])){*
*		  $form['fields']['title']['description'] = '(max 60 charactres)';*
*		}*
*		if(isset($form['fields']['price'])){*
*		  $form['fields']['price']['description'] = '(per listing)';*
*		}*
*		if(isset($form['fields']['price_extras'])){*
*		  $form['fields']['price_extras']['description'] = 'Specify any extra items offered at an additional charge.';*
*		}*
*		if(isset($form['fields']['description'])){*
*		  $form['fields']['description']['description'] = '(max 600 charactres)';*
*		}*
*		if(isset($form['fields']['purchase_note'])){*
*		  $form['fields']['purchase_note']['description'] = 'Note that will be revealed at purchase.';*
*		}		*
*		if(isset($form['fields']['booking_offset'])){*
*		  $form['fields']['booking_offset']['label'] = 'PURCHASE OFFSET';*
*		}*
*		if(isset($form['fields']['booking_offset'])){*
*		  $form['fields']['booking_offset']['description'] = 'How many days are required prior purchase date.';*
*		}*
*		if(isset($form['fields']['booking_days'])){*
*		$form['fields']['booking_days']['description'] = 'Day/s available for purchase.';*
*		}*
*				*
*		return $form;*
*	},*
*	1000*
*);*

**Fields unset:**
*add_filter(*
*	'hivepress/v1/forms/listing_update',*
*	function( $form ) {*
*		unset( $form['fields']['booking_window'] );*
*		unset( $form['fields']['booking_min_time'] );*
*		unset( $form['fields']['booking_max_time'] );*
*		unset( $form['fields']['booking_min_quantity'] );*
*		unset( $form['fields']['booking_max_quantity'] );*
*		unset( $form['fields']['booking_slot_duration'] );*
*		unset( $form['fields']['booking_slot_interval'] );*
*		unset( $form['fields']['booking_moderated']);*
*		//unset( $form['fields']['booking_min_length'] );*
*		//unset( $form['fields']['booking_max_length'] );*
*		return $form;*
*	},*
*	1000*
*);*

**Extras field option:**
*add_filter(*
*	'hivepress/v1/models/listing/attributes',*
*	function($attributes){*
*		if(isset($attributes['price_extras'])){*
*			$attributes['price_extras']['edit_field']['fields']['type']['options'] = [*
*				'per_order' => 'per item',*
*			];*
*		}*
*		*
*		return $attributes;*
*	},*
*	1000*
*);*

*add_filter( 'hivepress/v1/models/listing/fields', 'change_price_extras_custom', 200, 2 );*
*add_filter( 'hivepress/v1/forms/listing_update', 'change_price_extras_custom', 200, 2 );*
*add_filter( 'hivepress/v1/meta_boxes/listing_attributes', 'change_price_extras_custom', 200 );*
*add_filter( 'hivepress/v1/models/vendor/fields', 'change_price_extras_custom', 200, 2 );*
*add_filter( 'hivepress/v1/forms/vendor_update', 'change_price_extras_custom', 200, 2 );*
*add_filter( 'hivepress/v1/meta_boxes/vendor_attributes', 'change_price_extras_custom', 200 );*

*function change_price_extras_custom($form, $model = null){*
*		$is_form    = strpos( current_filter(), 'form' );*
*		$is_model   = strpos( current_filter(), 'model' );*
*		$is_listing = strpos( current_filter(), 'listing' );*
*		$per_vendor = get_option( 'hp_booking_per_vendor' );*

*		if ( ! $is_listing && ! $per_vendor ) {*
*			return $form;*
*		}*
*		$fields = [];*

*		if ( $is_model ) {*
*			$fields = $form;*
*		} else {*
*			$fields = $form['fields'];*
*		}*
*		$listing_id = null;*

*		if ( $is_listing ) {*
*			if ( $is_model ) {*
*				$listing_id = $model->get_id();*
*			} elseif ( $is_form ) {*
*				$listing_id = $model->get_model()->get_id();*
*			} else {*
*				$listing_id = get_the_ID();*
*			}*

*			if ( ! $listing_id || ! hivepress()->booking->is_booking_enabled( $listing_id ) ) {*
*				return $form;*
*			}*
*		}*


*		if ( $is_listing && hivepress()->get_version( 'marketplace' ) ) {*
*			if ( get_option( 'hp_listing_allow_price_extras' ) ) {*
*				$fields['price_extras']['fields']['type'] = [*
*					'type'    => 'select',*
*					'options' => [*
*						'per_order' => 'per extra',*
*					],*
*					'_order'  => 30,*
*				];*
*			}*
*		}*
*		if ( $is_model ) {*
*			$form = $fields;*
*		} else {*
*			$form['fields'] = $fields;*
*		}*

*		return $form;*
*}*

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.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.