Cannot redeclare two functions

How can I solve the following issue with the code attached? Seems that somehow WordPress (or some kind of plugin) is calling the function twice.

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

Fatal error: Cannot redeclare ov3rfly_replace_include_blank() (previously declared in /Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php:21) in /Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php on line 21

Related posts

2 comments

  1. Dont nest the functions – your current code declares the inner function every time the outer function is called, thereby causing the error the second time:

    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');
    
  2. Check this file for re declare function as error message suggested

    /Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php:21
    

    rename one function and see whether it working or not

    Write a separate function,multiple function calling in nested function:

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

Comments are closed.