• I have a website with wpautop disabled for WPCF7 forms and enabled for their emails, because some forms on the website have emails which need wpautop for their correct presentation. Some other forms have an email that doesn’t need wpautop. I think that this should not be a problem if these are full-html body-content emails (with in the editor the option “Use html content type” enabled), right?

    However, that is not the case. For example, suppose the email content contains a table with a cell <td>[your_name]</td> . When I process the email content with the WordPress function wpautop, outside the WPCF7 plugin,, I get the same text, albeit split over three lines: opening tag, cell content, closing tag:
    <td>
    [your_name]
    </td>
    That is perfectly all right.

    The email produced by WPCF7, however, contains (when I put everything back on one line): <td><p>[your_name]</p></td>. As a consequence, the cell content is presented in the email received with paragraph styling, resulting in 32px margin between the table rows.

    Is there a way to prevent this, other than adding a <style> element to the email content to (in this case) reset the <p> styling? Or is there a way to set the email wpautop on/off specific for each form? I checked $_POST in a wpcf7_autop_or_not filter function, but I didn’t find the information for which form it was going to send an email.

    Thank you in advance for your help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Henk Barreveld

    (@henk-barreveld)

    In the meantime I have looked into the Contact Form 7 code and found that the plugin does not use wpautop at all; it has its own function wpcf7_autop. So the title of this request is incorrect, wpautop is not involved.

    I tried to solve my problem with the unnecessary <p> tags added by wpcf7_autop in the emails, by disabling wpcf7_autop everywhere with
    add_filter( 'wpcf7_autop_or_not', '__return_false' )
    and doing a wpautop call myself for the email body created by Contact Form 7, in a callback function for the filter wpcf7_mail_html_body. This seems to be working all right.

    Is there anything wrong with this approach?

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    disabling wpcf7_autop everywhere with
    add_filter( 'wpcf7_autop_or_not', '__return_false' )
    and doing a wpautop call myself for the email body

    You can do it with just one call:

    add_filter( 'wpcf7_autop_or_not', static function ( $autop, $options ) {
        $autop = false;
    
        if ( 'mail' === $options['for'] ) {
            $autop = true;
        }
    
        return $autop;
    }, 10, 2 );
    Thread Starter Henk Barreveld

    (@henk-barreveld)

    Hello @takayukister,

    Thank you very much for your explanation and the necessary code. However, I think there may be a misunderstanding. Using the code in your reply, forms will not be ‘autop-ed’ and emails would be ‘autop-ed’ by the function wpcf7_autop, correct?

    For forms this all right, but for emails I want to use the standard WordPress wpautop function instead of wpcf7_autop, because wpcf7_autop creates some undesired results. For example, it creates unnecessary p-tags in table cells; the wpautop function doesn’t.

    To process the email body with wpautop I use the filter wpcf7_mail_html_body, introduced in release 5.8, but I am not completely confident that this is the appropriate way to do it.

    Thanks again.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    That’s my misunderstanding. Sorry!

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.