Filter and Hooks

Filter to set blank value instead of default “—” in the drop down list of contact form 7

  1. Replace all ‘---' in all forms with the same text:

For replacing all  ‘—‘  blank value of   select , add the  below filter in function.php

function kd_wpcf7_form_elements($html) {
    $text = 'Select';
    $html = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $html);
    return $html;
}
add_filter('wpcf7_form_elements', 'kd_wpcf7_form_elements');

We can replace the  $text variable with any text.

2.  Replace the select element blank value with  unique text.

For replacing the select elements blank with unique text add the following filter in function.php

function kd_wpcf7_form_elements($html) {
    function kd_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);
        }
    }
    kd_replace_include_blank('subject', 'Subject', $html);
    kd_replace_include_blank('country', 'Country', $html);
    return $html;
}
add_filter('wpcf7_form_elements', 'kd_wpcf7_form_elements');

1

Views: 1029

Standard