首页 文章

无法重新声明两个功能

提问于
浏览
1

如何使用附加的代码解决以下问题?似乎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申报)/ XAMPP / xamppfiles / htdocs中/ W /第21行的wp-content / themes / bulwark_child / functions.php

2 回答

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

相关问题