Add customized fields in Contact Form 7

wpcf7_init

Add a field in Contact Form 7.

In the form, insert

[favorite_posts post_titles]

In the email content, use

[post_titles]

In functions.php,

add_action( 'wpcf7_init', 'custom_add_form_tag_form_seminars' );
 
function custom_add_form_tag_form_seminars() {
    wpcf7_add_form_tag( 'favorite_posts', 'your_form_shortcode', array('name-attr' => true) ); // "form_seminars" is the type of the form-tag
}


function your_form_shortcode( $tag ) {
    if (!is_object($tag)) return '';
    $name = $tag->{'name'};
    if (empty($name)) return '';

    ob_start();
    $args = array('post_type' => 'post', 'numberposts' => -1 );

    $the_query = new WP_Query( $args ); 
    if ( $the_query->have_posts() ) {
?>

    <table class="table table-bordered">

<?php
        while ( $the_query->have_posts() ) { $the_query->the_post();
?>
        <tr>
            <td style="width:40px;">
                <input type="checkbox" id="hoge_<?php the_ID(); ?>" name="<?php echo $name; ?>[]" class="wpcf7-form-control" value="<?php echo esc_attr( get_the_title() ); ?>" >
            </td>
            <td><label for="hoge_<?php the_ID(); ?>"><?php the_title(); ?></label></td>
        </tr>   
<?php
        }
?>
    </table>

<?php
    }

    $page = ob_get_contents();
    ob_end_clean();
    return $page;   
}

The key is setting array(‘name-attr’ => true ) in your wpcf7_add_form_tag() and set class=”wpcf7-form-control” in your input.