Contact Form 7 in Wordpress - How can I hide incom

2019-07-30 09:16发布

问题:

Can anyone help me find a way to hide fields in the e-mail sent?

Basically I have multiple rows (like a multiple order list) and if the user only fills in the top row I do not want the other empty rows to show at all in the e-mail.

Here's a section of code for the form (but there are 8 other rows i.e. 10 orders):

<tr class="stationary-order-input">
        <td>[text order-1-page-number]</td>
        <td>[text order-1-item-number]</td>
        <td>[text order-1-item-description]</td>
        <td>[text order-1-quantity]</td>
    </tr>
    <tr class="stationary-order-input">
        <td>[text order-2-page-number]</td>
        <td>[text order-2-item-number]</td>
        <td>[text order-2-item-description]</td>
        <td>[text order-2-quantity]</td>
    </tr>

And here's a section of how my e-mail is set (up to ORDER 10):

ORDER 1 - Details:

Page number: [page-number]
Item number: [item-number]
Item description: [item-description]
Quantity: [quantity]

ORDER 2 - Details:

Page number: [order-2-page-number]
Item number: [order-2-item-number]
Item description: [order-2-item-description]
Quantity: [order-2-quantity]

Currently, all 10 orders will show in e-mail even though the user hasn't completed all of them. So how can I hide these incomplete fields in the e-mail?

Please help!

回答1:

Try the following filter, haven't tested in a live site and my local site doesn't send emails, so I'm not sure.

As CF7 has some internal values, I think it's best to check for each field individually, e.g., your-address, your-phone, etc:

add_filter( 'wpcf7_posted_data', 'cf7_so_15007502' );

function cf7_so_15007502( $posted_data )
{
    if( isset( $posted_data['your-address'] ) && '' == $posted_data['your-address'] )
        unset( $posted_data['your-address'] );

    if( isset( $posted_data['your-phone'] ) && '' == $posted_data['your-phone'] )
        unset( $posted_data['your-phone'] );

    return $posted_data;
}


回答2:

In the mail 1 field in contact form 7 there is a check box at the bottom of the email field. it states Exclude lines with blank mail-tags from output put a check mark on it. then save form

now when clients fill out the form and don't fill some fields it will not show in your email.



回答3:

Here a more dynamic way to remove Contact Form 7 tags from mail.

function on_wpcf7_mail_components( $data, $form, $mail )
{        
    foreach ( (array) $form->form_scan_shortcode() as $shortcode )
    {
        if ( empty( $shortcode['name'] ) )
        {
            continue;
        }

        $tag = sprintf( '[%s]', $shortcode['name'] );

        $data['body'] = str_replace( $tag , '', $data['body'] );
    }

    return $data;
}

add_filter( 'wpcf7_mail_components', 'on_wpcf7_mail_components', 10, 3 );


回答4:

Simply check the checkbox parallel to the text "Exclude lines with blank mail-tags from output", under the message body field.