这是测试站点的网址 - http://gil.beastserv.com/hava/
我有这个功能,这里有一些帮助
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('c_age', 'גיל', $html);
ov3rfly_replace_include_blank('c_area', 'איזור', $html);
ov3rfly_replace_include_blank('c_type', 'סוג ביטוח', $html);
ov3rfly_replace_include_blank('c_area', 'איזור', $html);
ov3rfly_replace_include_blank('c_cartype', 'סוג רכב', $html);
ov3rfly_replace_include_blank('c_manifacture', 'יצרן', $html);
ov3rfly_replace_include_blank('c_manifactureyear', 'שנת יצור', $html);
ov3rfly_replace_include_blank('c_driversage', 'גיל הנהג', $html);
ov3rfly_replace_include_blank('c_prevent', 'שלילות', $html);
ov3rfly_replace_include_blank('c_claim', 'תביעות', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');
当只有 1 个表单时,它工作得很好,但是当我试图在 2 个页面中放置 1 个表单时,我会收到致命错误:Cannot redeclare ov3rfly_replace_include_blank() (previously declared
我试图放置if (!function_exists('formatStr')) {}
如下:
if (!function_exists('formatStr')) {
function my_wpcf7_form_elements($html) {}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');
}
但我想这不是问题......该函数被调用两次,因为有两种形式..我该如何克服这一点?
多谢。
从 http://es2.php.net/manual/en/functions.user-defined.php:
PHP 中的所有函数和类都具有全局范围 - 它们可以是 在函数外部调用,即使它们是在函数内部和 Vice 中定义的 反之亦然。
PHP 不支持函数重载,也不可能 取消定义或重新定义以前声明的函数。
每次调用my_wpcf7_form_elements
时都会声明函数ov3rfly_replace_include_blank
,并且PHP不支持函数重载,因此会出现错误。由于所有PHP函数都具有全局作用域,因此即使在函数外部定义,也可以在函数内部调用它们。尝试:
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);
}
}
function my_wpcf7_form_elements($html) {
ov3rfly_replace_include_blank('c_age', 'גיל', $html);
ov3rfly_replace_include_blank('c_area', 'איזור', $html);
ov3rfly_replace_include_blank('c_type', 'סוג ביטוח', $html);
ov3rfly_replace_include_blank('c_area', 'איזור', $html);
ov3rfly_replace_include_blank('c_cartype', 'סוג רכב', $html);
ov3rfly_replace_include_blank('c_manifacture', 'יצרן', $html);
ov3rfly_replace_include_blank('c_manifactureyear', 'שנת יצור', $html);
ov3rfly_replace_include_blank('c_driversage', 'גיל הנהג', $html);
ov3rfly_replace_include_blank('c_prevent', 'שלילות', $html);
ov3rfly_replace_include_blank('c_claim', 'תביעות', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');