Search code examples
wordpresshookcontact-form-7

Contact Form 7: wpcf7_form_response_output filter


May I ask how is the wpcf7_form_response_output filter (found in includes/contact-form.php) use?

The below is what I had tried but to no avail.

============== Case 1 ============

Function 1:

$wpcf7->skip_mail = true;
$content = __('You have already subscribed to our Events.',CURRENT_THEME);
$class = 'wpcf7-validation-errors';
custom_wpcf7_form_response_output_filter('', $class, $content, '');

Function 2

if(!function_exists('custom_wpcf7_form_response_output_filter')){
    function custom_wpcf7_form_response_output_filter($output, $class, $content, $this){
        return '<div class="' . $class . ' ">' . $content . '</div>';
    }
    add_filter( 'wpcf7_form_response_output', 'custom_wpcf7_form_response_output_filter', 10, 4);
}

========== Case 2 ==============

$wpcf7->skip_mail = true;
$class = 'wpcf7-validation-errors';
$error = __('You have already subscribed to our Events.',CURRENT_THEME);
$output = '<div class="' . $class . ' ">' . esc_html( $error ) . '</div>';
$omsg = 'return "'. $output .'";';
add_filter('wpcf7_form_response_output', create_function('$a', $omsg));

Case 2: Another method which does not work as well. 500 Internal Server Error when used.

Any help here?


Solution

  • It looks like the hook is not attached / triggering at the right time.

    EDIT

    The hook described in this thread does not trigger reliably when making an AJAX call. We decided to do it with JS instead. Here is some code that will help anyone else looking/facing a similar issue

    $( document ).ajaxComplete(function() {
        str=$(".wpcf7-response-output").html();
        if (str.indexOf("error") >= 0)
            $(".wpcf7-response-output")
                .removeClass("wpcf7-mail-sent-ok")
                .addClass("wpcf7-validation-errors");
        });