Block Links in WPForms Paragraph Fields
Shows an error when a WPForms paragraph text field contains web links, which stops most link spam without adding a captcha.
Opens WPIDE on
Code
$wpide_wpf_max_links = 0;
add_action( 'wpforms_process_validate_textarea', function ( $field_id, $field_submit, $form_data ) use ( $wpide_wpf_max_links ) {
$links = preg_match_all( '#(https?://|www\.|\[url|<a\s)#i', (string) $field_submit );
if ( $links > $wpide_wpf_max_links ) {
$process = method_exists( wpforms(), 'obj' ) ? wpforms()->obj( 'process' ) : wpforms()->process;
$process->errors[ $form_data['id'] ][ $field_id ] = 0 === $wpide_wpf_max_links ? 'Please remove the links from your message.' : sprintf( 'Please include no more than %d link(s).', $wpide_wpf_max_links );
}
}, 10, 3 );
Notes
Set the number of links you allow at the top if customers often share a link.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.