For years I have used some code given by GF that prevents form submissions if URLs are included in specific (or any) form fields (to stop spam bots), but it has suddenly stopped working and now triggers a fatal error:
Fatal error : Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in […]
Here is the code I was given, the line that is triggering the error is the one that starts with "if ( $result[‘is_valid’] "
add_filter( 'gform_field_validation', 'custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {
if ( $result['is_valid'] && preg_match('/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i', $value ) ) {
$result['is_valid'] = false;
$result['message'] = 'Please remove URLs';
}
return $result;
}
I found very similar code here that also does not work (triggers the same error):
https://community.gravityforms.com/t/how-to-block-link-tags-and-urls-hyperlinks-contained-in-any-form-field-regex-format-resolved/13787
Is there an updated way to stop users from submitting hyperlinks in form fields?
I am using the most current version of Gravity Forms with WP ver. 6.1.1 and PHP 8.0.
Any help would be appreciated!