• Hello, I need to clean the textarea message of a CF7 form in order to avoid multiple lines in the text.

    I use the following function:

    function wpcf7_textarea_strip_tags($WPCF7_ContactForm)
    {
        //Get current form
        $wpcf7 = WPCF7_ContactForm::get_current();
        // get current SUBMISSION instance
        $submission = WPCF7_Submission::get_instance();
        // Ok go forward
        if ($submission) {
            // get submission data
            $data = $submission->get_posted_data();
            // nothing's here... do nothing...
            if (empty($data)) {
                return;
            }
    
            $text = isset($data['contact-form-message']) ? $data['contact-form-message'] : "";
            $text = preg_replace('/\s+/', ' ', $text);
            // or $text = preg_replace("/\r|\n/", " ", $text);
    
            $mail = $wpcf7->prop('mail');
            $mail['body'] = str_replace('[contact-form-message]', $text, $mail['body']);
            // Save the email body
            $wpcf7->set_properties(array(
                "mail" => $mail
            ));
            // return current cf7 instance
            return $wpcf7;
        }
    }
    add_action("wpcf7_before_send_mail", "wpcf7_textarea_strip_tags");

    This code is working for the email message from the “EMAIL 1”, but not from “EMAIL 2” (this one, still contain the message with breaklines).

    How can I solve this? I need this as the Email 2 is connected to an external CRM that parse each single lines as field values

Viewing 1 replies (of 1 total)
  • Thread Starter saxpaolo

    (@saxpaolo)

    Update:
    I’ve partially solved the problem updating the function above as follow:

    function wpcf7_textarea_strip_tags($WPCF7_ContactForm)
    {
        //Get current form
        $wpcf7 = WPCF7_ContactForm::get_current();
        // get current SUBMISSION instance
        $submission = WPCF7_Submission::get_instance();
        // Ok go forward
        if ($submission) {
            // get submission data
            $data = $submission->get_posted_data();
            // nothing's here... do nothing...
            if (empty($data)) {
                return;
            }
    
            $text = isset($data['contact-form-message']) ? $data['contact-form-message'] : "";
            $text = preg_replace('/\s+/', ' ', trim($text));
    
            // Mail 1
            $mail_1 = $wpcf7->prop('mail');
    
            // Mail 2
            $mail_2 = $wpcf7->prop('mail_2');
            $mail_2['body'] = str_replace('[contact-form-message]', $text, $mail_2['body']);
            
            // Save the email body
            $wpcf7->set_properties(array(
                "mail" => $mail_1,
                "mail_2" => $mail_2,
            ));
            // return current cf7 instance
            return $wpcf7;
        }
    }
    add_action("wpcf7_before_send_mail", "wpcf7_textarea_strip_tags");

    Now the EMAIL 2 body text is correctly cleaned (ie. te breakline have been replace with single spaces), but the body of the EMAIL 1 have been cleaned too, but here the breaklines have not been replaced with blank spaces, so there no spaces between words… What I’m doing wrong?

    • This reply was modified 3 years, 9 months ago by saxpaolo.
Viewing 1 replies (of 1 total)
  • The topic ‘Strip textarea breaklines on Email 2 message’ is closed to new replies.