Search code examples
htmlwordpresscontact-form-7elementor

allow html tags in contact form 7 success mesage (wpcf7-messages[mail_sent_ok])


i want to use html in success message.

i try this but not working.

add_filter('wpcf7_mail_sent_ok', 'my_succs_message_alter', 1, 2);
function my_succs_message_alter($message, $args){
    $message = htmlspecialchars_decode($message);
    return $message;
}

my purpose; after submit show elementor template. But if subject == "aa" show aa template, if subject == "bb" show bb template.. etc. I do it. I print it in success message. just one problem "allow html".


Solution

  • i found the answer:

    php:

    add_filter('wpcf7_display_message', function($msg){
      $custom_html_message = '<h2>html code..</h2>';
      return htmlentities($custom_html_message);
    }, 10, 2);
    

    js:

    var wpcf7Elm = document.querySelector( '.wpcf7' );
    wpcf7Elm.addEventListener( 'wpcf7submit', function( event ) {
        setTimeout(function(){
            const successMessage = document.querySelector('.wpcf7-response-output');
            const htmlMessage = htmlEntitiesDecode(successMessage.innerText);
            successMessage.innerHTML = htmlMessage;
        }, 200);
    }, false );
    
    function htmlEntitiesDecode(str) {
        return jQuery('<textarea />').html(str).text();
    }