Skip to content

Instantly share code, notes, and snippets.

@anythinggraphic
Last active January 3, 2021 21:02
Show Gist options
  • Save anythinggraphic/c5d9da935697df703ddd79883b1865f1 to your computer and use it in GitHub Desktop.
Save anythinggraphic/c5d9da935697df703ddd79883b1865f1 to your computer and use it in GitHub Desktop.
Gravity Forms - Allow certain HTML tags, remove link (a, href) tags, and prevent http and https from being submitted during validation.
<?php
/**
* Gravity Forms: Allowable tags.
* By returning specific HTML tags, we can prevent link tags from being submitted.
* The links will simply be stripped out without a warning to the user.
*
* @author Anything Graphic
* @link https://anythinggraphic.net
* @link https://community.gravityforms.com/t/how-to-block-links-in-text-field-resolved/5865/5
* @link https://docs.gravityforms.com/gform_allowable_tags/
* @param array $allowable_tags the string of default allowable tags, removing the anchor tag.
*/
function ag_allow_basic_tags( $allowable_tags ) {
return '<p><strong><em>'; // No link tag, only excepting paragraphc, strong, and emphasis HTML tags.
}
add_filter( 'gform_allowable_tags', 'ag_allow_basic_tags' );
/**
* Gravity Forms: Form Validation.
*
* The below looks for the words 'http' and 'https' upon after the user tries to submit the form.
* During form validation, if the words appear within the form, the form will not submit.
* The user will then be presented with a custom, informative, and actionable message.
*
* @author Anything Graphic
* @link https://anythinggraphic.net
* @link https://docs.gravityforms.com/gform_field_validation
* @param array $result The validation result to be filtered.
* @param string $value The field value to be validated. Multi-input fields like Address will pass an array of values.
* @param object $form Current form object.
* @param object $field Current field object.
*/
function ag_disable_urls_validation( $result, $value, $form, $field ) {
$nourl_pattern = '(http|https)'; // Looks for http and https.
if ( preg_match( $nourl_pattern, $value ) ) {
$result['is_valid'] = false;
$result['message'] = 'Message can not contain website addresses.';
}
return $result;
}
add_filter( 'gform_field_validation', 'ag_disable_urls_validation', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment