Created
March 15, 2018 16:09
-
-
Save radist2s/dbb53e9506a301eab44572e106ed7893 to your computer and use it in GitHub Desktop.
WordPress Contact Form 7 custom chars validation hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
<? | |
// Hooks only for version 5 only, may be for previous version also works | |
if (!defined('WPCF7_VERSION') OR intval(WPCF7_VERSION) !== 5) | |
{ | |
return; | |
} | |
add_filter( 'wpcf7_validate_text', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 ); | |
add_filter( 'wpcf7_validate_text*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 ); | |
add_filter( 'wpcf7_validate_email', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 ); | |
add_filter( 'wpcf7_validate_email*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 ); | |
add_filter( 'wpcf7_validate_url', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 ); | |
add_filter( 'wpcf7_validate_url*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 ); | |
add_filter( 'wpcf7_validate_tel', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 ); | |
add_filter( 'wpcf7_validate_tel*', 'wpcf7_text_allowed_chars_validation_filter', 11, 2 ); | |
/** | |
* @param WPCF7_Validation $result | |
* @param WPCF7_FormTag $tag | |
* @return mixed | |
*/ | |
function wpcf7_text_allowed_chars_validation_filter($result, $tag) | |
{ | |
$name = $tag->name; | |
$value = isset($_POST[$name]) | |
? trim(wp_unslash(strtr((string)$_POST[$name], "\n", " "))) | |
: ''; | |
if ($validation_patterns = $tag->get_option('valid-chars')) | |
{ | |
$validation_patterns = wpcf7_field_validation_patterns_filter($validation_patterns); | |
} | |
if ($validation_patterns) | |
{ | |
if (!wpcf7_validate_field_with_pattern($validation_patterns, $value)) | |
{ | |
$result->invalidate($tag, wpcf7_get_message(wpcf7_get_field_invalidation_pattern_message($validation_patterns))); | |
} | |
} | |
return $result; | |
} | |
function wpcf7_get_default_validation_patterns() | |
{ | |
return $patterns = [ | |
'letters' => 'A-ZА-Я\s_\-', | |
'digits' => '\d' | |
]; | |
} | |
function wpcf7_field_validation_patterns_filter($patterns = []) | |
{ | |
$valid_patterns = wpcf7_get_default_validation_patterns(); | |
return array_filter($patterns, function ($pattern) use ($valid_patterns) | |
{ | |
return !empty($valid_patterns[$pattern]); | |
}); | |
} | |
function wpcf7_get_field_invalidation_pattern_message($patterns = []) | |
{ | |
$is_letters_invalid = in_array('letters', $patterns); | |
$is_digits_invalid = in_array('digits', $patterns); | |
if ($is_letters_invalid AND $is_digits_invalid) | |
{ | |
return 'invalid_letters_digits'; | |
} | |
elseif ($is_letters_invalid) | |
{ | |
return 'invalid_letters'; | |
} | |
elseif ($is_digits_invalid) | |
{ | |
return 'invalid_digits'; | |
} | |
} | |
/** | |
* @param string[] $validation_patterns | |
* @param string $value | |
* @return bool | |
*/ | |
function wpcf7_validate_field_with_pattern($validation_patterns = [], $value) | |
{ | |
$validation_patterns = array_filter((array)$validation_patterns); | |
if (!$validation_patterns) | |
{ | |
return TRUE; | |
} | |
$patterns = wpcf7_get_default_validation_patterns(); | |
$pattern = []; | |
foreach ($validation_patterns AS $pattern_name) | |
{ | |
if (isset($patterns[$pattern_name])) | |
{ | |
$pattern[] = $patterns[$pattern_name]; | |
} | |
} | |
$pattern = implode('', $pattern); | |
return preg_match("/^[$pattern]+$/iu", $value); | |
} | |
add_filter('wpcf7_messages', function ($messages) | |
{ | |
$messages['invalid_letters_digits'] = [ | |
'description' => __('Allowed letters and digits only.', 'THEME_DOMAIN'), | |
'default' => __('Letters and didgits only.', 'THEME_DOMAIN'), | |
]; | |
$messages['invalid_letters'] = [ | |
'description' => __('Allowed letters only.', 'THEME_DOMAIN'), | |
'default' => __('Letters only.', 'THEME_DOMAIN'), | |
]; | |
$messages['invalid_digits'] = [ | |
'description' => __('Allowed digits only.', 'THEME_DOMAIN'), | |
'default' => __('Digits only.', 'THEME_DOMAIN'), | |
]; | |
return $messages; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Exmaple of shortcode implementation --> | |
[text surname valid-chars:letters] | |
[text sku valid-chars:letters valid-chars:digits] | |
<!-- | |
Only letters and digits validation implemented, | |
just extend `wpcf7_get_default_validation_patterns()` | |
and validation message checker `wpcf7_get_field_invalidation_pattern_message()` | |
Of course, add messages into hook `wpcf7_messages` | |
--> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment