phpwordpressreplacefieldcontacts

Replace the Select fields in Wordpress plugin Contact Form 7


I found this code right here in stackoverflow which to me is very useful, it gives replace the select fields of the Contact Form 7 by placing an initial value like a placeholder.

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('sexo', 'Sexo*', $html);
ov3rfly_replace_include_blank('civil', 'Estado Civil*', $html);
ov3rfly_replace_include_blank('escolaridade', 'Escolaridade*', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');

The problem is that when I put a form in widget displays the error:

Fatal error: Cannot redeclare ov3rfly_replace_include_blank() (previously declared in /home/storage/7/70/5b/mysite/public_html/wp-content/themes/mytheme/functions.php:65) in /home/storage/7/70/5b/mysite/public_html/wp-content/themes/mytheme/functions.php

The line 65 is this:

function ov3rfly_replace_include_blank($name, $text, &$html) {

Someone can help solve it? Like, does not run if it is inside a widget.

Because the form I'm trying to put the widget has no Select fields.

Thank you all for the attention.


Solution

  • You cannot redeclare a already declared function as the error clearly says. Make sure you have the needed declaration and delete or comment out the other declarations of the function 'ov3rfly_replace_include_blank'.

    I would suggest you to use function_exists() php function to check if the function has been declared before or not. Something like this:

    if (!function_exists('ov3rfly_replace_include_blank')) {
        function ov3rfly_replace_include_blank(args.....){
            //Logic goes here.....
        }
    }