“fatal error: Cannot redeclare” while changing “—” title on <select> element in contact form 7 function

this is the url to the test site – http://gil.beastserv.com/hava/

i have this function, with some help from here

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

its working great when there is only 1 form, but when im tring to place 2 forms in 1 page, im getting the Fatal error: Cannot redeclare ov3rfly_replace_include_blank() (previously declared

i tried to place the if (!function_exists('formatStr')) {}
as so:

if (!function_exists('formatStr')) {
function my_wpcf7_form_elements($html) {}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');
}

but i guess that its not the problem.. the function is called twice, because there are 2 forms.. how can i overcome this?

Thanks alot.

Related posts

Leave a Reply

1 comment

  1. From http://es2.php.net/manual/en/functions.user-defined.php:

    All functions and classes in PHP have the global scope – they can be
    called outside a function even if they were defined inside and vice
    versa.

    PHP does not support function overloading, nor is it possible to
    undefine or redefine previously-declared functions.

    function ov3rfly_replace_include_blank is being declared every time you call my_wpcf7_form_elements, and PHP does not support function overloading, hence the error. Since all PHP functions have global scope, they can be called inside a function even if they where defined outside. Try:

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