我如何解决以下问题与代码附件?似乎是Wordpress(或某种插件)调用了这个函数两次。
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('countrylist', 'España', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');
致命错误:无法重新声明ov3rfly_replace_include_blank()(先前声明在/Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php:21)在/Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php第21行
不要嵌套函数-当前代码在每次调用外部函数时声明内部函数,从而导致第二次错误:
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('countrylist', 'España', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');
检查此文件是否存在重新声明函数的错误信息
/Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php:21
重命名一个函数,看看它是否工作
编写一个单独的函数,在嵌套函数中调用多个函数:
function my_wpcf7_form_elements($html) {
ov3rfly_replace_include_blank('countrylist', 'España', $html);
return $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);
}
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');