【发布时间】:2019-04-25 00:59:25
【问题描述】:
WordPress - 联系表格 7
当有人在其中输入值时,我正在尝试找出用于修改 cf7 字段值的过滤器。
当用户在文本字段中输入并提交数据时,
- 验证 - 我已经完成了
- 如果输入无效,则不应转到感谢页面 - 我已经完成了
- 用新数据替换文本字段 - 未完成
例如:1
add_filter( 'wpcf7_validate_text*', 'your_validation_filter_func_tel', 100, 2 );
function your_validation_filter_func_tel( $result, $tag ) {
$Yourvalue = $_POST['your-number'];
if ( strlen( $Yourvalue ) == 2 ) {
$result->invalidate( 'your-number', "Please enter a valid number. " . );
// HERE I WANT TO REPLACE NEW DATA TO TEXT FIELD
$result->data( 'your-number', '1002' );
} else if ( strlen( $Yourvalue ) == 3 ) {
$result->invalidate( 'your-number', "Please enter a valid name." . );
// HERE I WANT TO REPLACE NEW DATA TO TEXT FIELD
$result->data( 'your-number', '1003' );
}
return $result;
}
例如:2
另一个工作示例
除了$result['tel'] = $tel_cleaned_final; 之外的一切正常
<?php
function custom_filter_wpcf7_is_tel( $result, $tel )
{
// Initialization and Clean Input
$tel_cleaned = strtr( $tel, array(' '=>'', '-'=>'', '.'=>''));
$tel_cleaned_trimmed = ltrim(strtr( $tel_cleaned, array('+'=>'')), '0');
/* Test Conditions.
I concluded 3 if conditions to 1 below bcaz the validation is working perfect
*/
if ('test conditions here')
$tel_cleaned_final = substr($tel_cleaned_trimmed, 2);
else
$tel_cleaned_final = $tel_cleaned_trimmed;
if (strlen($tel_cleaned_final) == 10)
{
$result = true;
//$result['tel'] = $tel_cleaned_final;
/*
Here i want to return new number to text box
for eg: +91 98989-89898 returns 9898989898
*/
}
else
{
$result = false;
}
return $result;
}
add_filter( 'wpcf7_is_tel', 'custom_filter_wpcf7_is_tel', 10, 2 );
?>
【问题讨论】:
-
您的问题非常广泛。我建议查看official documentation 和hook documentation
-
@janw 我要的很简单,实例名改表单值
-
@J.K 你能分享你的联系表格代码吗?
-
@MukeshPanchal 请参见例如:2
-
Contact Form 7 的操作挂钩不允许更改输入字段。您必须添加一个触发错误的 DOM 事件,然后根据需要通过 JavaScript 检查和更新 DOM 中的字段。见contactform7.com/dom-events
标签: php wordpress contact-form-7