我在 stackoverflow 中找到了这段代码,这对我来说非常有用,它通过放置一个初始值(如占位符)来替换 Contact Form 7 的选择字段。
function my_wpcf7_form_elements($html) {
function ov3rfly_replace_include_blank($name, $text, &$html) {
$matches = false;
preg_match('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $html, $matches);
if ($matches) {
$select = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $matches[0]);
$html = preg_replace('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $select, $html);
}
}
ov3rfly_replace_include_blank('sexo', 'Sexo*', $html);
ov3rfly_replace_include_blank('civil', 'Estado Civil*', $html);
ov3rfly_replace_include_blank('escolaridade', 'Escolaridade*', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');
问题是当我将表单放入小部件时显示错误:
致命错误:无法在 /home/storage/7/ 中重新声明 ov3rfly_replace_include_blank() (之前在 /home/storage/7/70/5b/mysite/public_html/wp-content/themes/mytheme/functions.php:65 中声明) 70/5b/mysite/public_html/wp-content/themes/mytheme/functions.php
第 65 行是这样的:
function ov3rfly_replace_include_blank($name, $text, &$html) {
有人可以帮忙解决吗?就像,如果它在小部件内,则不会运行。
因为我尝试放置小部件的表单没有选择字段。
谢谢大家的关注。
您无法重新声明已声明的函数,因为错误明确指出了这一点。确保您具有所需的声明,并删除或注释掉函数“ov3rfly_replace_include_blank”的其他声明。
我建议你使用 function_exists() php 函数来检查该函数是否已经被声明过。像这样的东西:
if (!function_exists('ov3rfly_replace_include_blank')) {
function ov3rfly_replace_include_blank(args.....){
//Logic goes here.....
}
}