Hello,
I’m trying to add a few custom social link validation rules to the social links plugin, but i want to validate them so i use this filter to validate the input:
add_action(‘hivepress/v1/forms/user_update/errors’, [$this, ‘validate_social_links’], 1000, 2);
public function validate_social_links($errors, $form) {
$fields = $form->get_fields();
error_log(print_r($form->get_fields(), true));
$website = isset($_POST['website']) ? sanitize_text_field($_POST['website']) : '';
$facebook = isset($_POST['facebook']) ? sanitize_text_field($_POST['facebook']) : '';
$instagram = isset($_POST['instagram']) ? sanitize_text_field($_POST['instagram']) : '';
// Validate Instagram (strict domain check and ensure username is included if a link)
if ($instagram && !preg_match('/^(?:@?[\w\.]+|https?:\/\/(?:www\.)?instagram\.com\/[\w\.]+)$/i', $instagram)) {
$errors['instagram'] = __('Please enter a valid Instagram username or profile URL.', 'hivepress-social-links');
}
// Validate Facebook (accepts username, or valid Facebook URL with or without http/https)
if ($facebook && !preg_match('/^(?:[\w\.]+|(?:https?:\/\/)?(?:www\.)?facebook\.com\/(?:profile\.php\?id=\d+|[\w\.]+))$/i', $facebook)) {
$errors['facebook'] = __('Please enter a valid Facebook username or profile URL.', 'hivepress-social-links');
}
if ($website) {
if (!preg_match('/^(https?:\/\/)/i', $website)) {
$website = 'http://' . $website;
}
if (!filter_var($website, FILTER_VALIDATE_URL)) {
$errors['website'] = __('Please enter a valid website URL.', 'hivepress-social-links');
}
}
error_log(print_r($errors, true));
return $errors;
}
Even though the value is validated and the error is returned as it’s not the correct format, the attribute is saved. Is it because they are tied to the vendor model? If the value is not validated i don’t want the input saved as it’s not valid.
EDIT:
I decided to use custom fields to handle the validation, so got it working that way.