无法重新声明两个功能

时间:2015-10-23 09:55:23

标签: php wordpress

如何使用附加的代码解决以下问题?似乎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');
  

致命错误:无法重新声明/ Applications / XAMPP / xamppfiles中的ov3rfly_replace_include_blank()(先前在/Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php:21中声明)第21行/htdocs/w/wp-content/themes/bulwark_child/functions.php

2 个答案:

答案 0 :(得分:1)

将此文件作为重新声明功能检查为建议的错误消息

/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');

答案 1 :(得分:1)

不要嵌套函数 - 每次调用外部函数时,当前代码都会声明内部函数,从而导致错误第二次:

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');