我有一个运行重力形式的 WordPress 网站,我需要有关访问者的大量信息,他们擅长响应,但我有一个字段(这是强制性的)以某种方式跳过了强制性部分。因此,在 200 个条目中,只有 2 个回答了这个问题。
我似乎找不到解决方案 - 由于该字段是必填字段,因此他们不应该将该字段留空。
该怎么办?
您是否尝试过在“提交”按钮和该字段上使用条件逻辑,在填写该字段之前提交按钮不会显示?
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) { // 仅适用于单行文本或段落字段。 if ( $field->type == 'text' || $field->type == 'textarea' ) {
$has_url = false;
// The following regex is just an example, feel free to change it.
if ( preg_match ( '/www\.|http:|https:\/\/[a-z0-9_]+([\-\.]{1}[a-z_0-9]+)*\.[_a-z]{2,5}'.'((:[0-9]{1,5})?\/.*)?$/i', $value ) ) {
$has_url = true;
}
if ( true === $has_url ) {
$result['is_valid'] = false;
$result['message'] = empty( $field->errorMessage ) ? 'Sorry, URL not allowed!' : $field->errorMessage;
}
if ( $result['is_valid'] ) {
$stop_words = array( // List of words to not allow in lowercase.
'viagra',
'porn',
'sidenafil',
'seo',
'seo’s',
'sem',
'errors',
'error',
'domain',
'domains',
'sex',
'web development',
'website development',
'blog',
'blogs',
'keywords',
'keyword',
'backlinks',
'backlink',
'ranking',
'rankings',
'casino',
'ukraine',
'click',
'click here',
'link',
'marketing',
'search engines',
'serch engine',
'search engine',
'on-page',
'on page',
'onpage',
'off-page',
'off page',
'offpage',
'optimizations',
'optimization',
'alt',
'ppc',
'kolkata',
'title tag',
'meta tag',
'google ads',
'speedmode',
'webmaster',
'digital marketing',
'social media marketing',
'email marketing',
'internet marketing',
'web marketing',
'link building',
'search engine marketing',
'web design agencies',
'web design',
'godaddy',
'robertrhync',
'gemma',
'registry',
'<a',
'</a>',
'https:',
'http:',
'misshaskn',
'mega.nz',
'countries',
'nations',
'spokesman',
'creative',
'talents',
);
// Trim and lowercase the input.
$lower_value = strtolower( trim( $value ) );
// Split the input into words.
$words = preg_split( '/\s+/', $lower_value );
// Check if there's only one word and if it's in the stop words list.
if ( 1 === count( $words ) && in_array( $words[0], $stop_words, true ) ) {
GFCommon::log_debug( __METHOD__ . "(): Stop word detected as the only word in field id {$field->id}. Stop Word: {$words[0]}" );
$result['is_valid'] = false;
$result['message'] = 'Sorry, your submission contains a word that is not allowed.';
} else {
// Check if any stop word is part of the submitted text.
foreach ( $stop_words as $stop_word ) {
if ( false !== strpos( $lower_value, $stop_word ) ) {
GFCommon::log_debug( __METHOD__ . "(): Stop word detected in field id {$field->id}. Stop Word: {$stop_word}" );
$result['is_valid'] = false;
$result['message'] = 'Sorry, your submission contains a word that is not allowed.';
break;
}
}
}
}
}
return $result;
}, 10, 4);