contact form dropdown from post

Solutions on MaxInterview for contact form dropdown from post by the best coders in the world

showing results for - "contact form dropdown from post"
Dewayne
28 Aug 2017
1add_action( 'wpcf7_init', 'custom_add_form_tag_customlist' );
2
3function custom_add_form_tag_customlist() {
4    wpcf7_add_form_tag( array( 'customlist', 'customlist*' ), 
5'custom_customlist_form_tag_handler', true );
6}
7
8function custom_customlist_form_tag_handler( $tag ) {
9
10    $tag = new WPCF7_FormTag( $tag );
11
12    if ( empty( $tag->name ) ) {
13        return '';
14    }
15
16    $customlist = '';
17
18    $query = new WP_Query(array(
19        'post_type' => 'CUSTOM POST TYPE HERE',
20        'post_status' => 'publish',
21        'posts_per_page' => -1,
22        'orderby'       => 'title',
23        'order'         => 'ASC',
24    ));
25
26    while ($query->have_posts()) {
27        $query->the_post();
28        $post_title = get_the_title();
29        $customlist .= sprintf( '<option value="%1$s">%2$s</option>', 
30esc_html( $post_title ), esc_html( $post_title ) );
31    }
32
33    wp_reset_query();
34
35    $customlist = sprintf(
36        '<select name="%1$s" id="%2$s">%3$s</select>', $tag->name,
37    $tag->name . '-options',
38        $customlist );
39
40    return $customlist;
41}
42
43//use this tag in your form
44//[customlist your-field-name]
45
46
47