Disable display of phone in listing description

Hi,

We need to filter phone numbers on text fields.

We tried to use this like in textarea, but with no success, do you have any alternative?

/* Filter Phone Numbers on Text */
add_filter(
	'hivepress/v1/fields/text/display_value',
	function( $value, $field ) {
		
		$value = preg_replace('/([0-9]{4})/', '*', $value);
		$value = preg_replace('/([0-9]+[\- ]?[0-9]+[\- ]?[0-9]+[\- ]?[0-9]+[\- ])/', '*', $value);
		

		return $value;
	},
	1000,
	2
);

Hi! You want to replace the numbers with stars (*) ?

Hi,

We recommend using the phone type attribute, it has validation, and you can additionally select codes, please check this doc How to add listing attributes - HivePress Help Center

We have a validation on textarea, so our vendors cannot add phone numbers, we need to filter this on our website, for example:


In here you can see that a vendor add phone numbers on a textarea field, and we cannot allow this.
For cleaning phone numbers on textarea fields I’m using the function display_value, what I show on the beggining, it will replace numbers for *.
But on a text field there is no display_value, how can we do this on text fields?
Regards

Hi,

Can you please provide more details why you can’t create a new field as an attribute and select phone type? Because it would be the easiest solution that doesn’t require any additional code. Regarding the description, you can disable the phone from being specified there using a PHP snippet.

Hi,

Sorry for misunderstanding, but we have a problem, our users are putting phone numbers on description, and that is forbidden.
They cannot give that information, they only shoud use the chat of our application, not sending phone numbers to contact clients.

Please try this PHP code snippet. But please note that it can require further customization

add_filter(
 'hivepress/v1/forms/listing_update/errors',
 function($errors, $form){
  if(!empty( $errors )){
   return $errors;
  }
  
  $text = $form->get_value('description');
  
  if(!$text){
   return $errors;
  }
  
  preg_match_all('/\\+?\\d{1,4}?[-.\\s]?\\(?\\d{1,3}?\\)?[-.\\s]?\\d{1,4}[-.\\s]?\\d{1,4}[-.\\s]?\\d{1,9}$/', $text, $matches);
  
  if(!array_filter($matches)){
   return $errors;
  }
  
  $errors[] = 'Phone numbers are not allowed in the text';
  return $errors;
 },
 1000,
 2
);

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