Iliya Reyzis Iliya Reyzis - 1 year ago 73
PHP Question

Contact Form 7 watermark for select menu

im searching for a way to put a watermark on select fields.

that is not working ->

[select* c_type class:ic watermark "choose type" "a" "b" "c"]

to put a not valid value that fail validation, i had to put

[select* c_type class:ic include_blank "a" "b" "c"]

but the problem is that i have
as watermark, thats what i want to change..

Answer Source

After hard searching i found this script that is working and replacing the "---" when targeting to that element this one is changing all the "---"s

function my_wpcf7_form_elements($html) {
    $text = 'Please select...';
    $html = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $html);
    return $html;
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');

this code, replacing with targeting

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('menu-569', 'Choose language', $html);
    ov3rfly_replace_include_blank('menu-614', 'Choose country', $html);
    return $html;
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');

hope that will save for some of you a headache (source here)