Contact Form 7 and Custom post type

前端 未结 2 1418
粉色の甜心
粉色の甜心 2021-01-12 11:49

I want to use contact form 7 in Wordpress to build a order Form. I want the content of the order Form to be populated with content from a custom post type \"trade Show Mater

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 11:58

    I do no think the wpcf7_form_tag works in the same way as vicente showed in his great answer before. It may have changed since 2015.

    If you read here it explains how you need to use the wpcf7_form_tag: https://contactform7.com/2015/01/10/adding-a-custom-form-tag/

    With that in mind along with this other post from Contact Form 7: https://contactform7.com/2015/02/27/using-values-from-a-form-tag/#more-13351

    I came up with this code to create a custom dropdown list for a custom post type that I have.

    add_action( 'wpcf7_init', 'custom_add_form_tag_customlist' );

    function custom_add_form_tag_customlist() {
        wpcf7_add_form_tag( array( 'customlist', 'customlist*' ), 
    'custom_customlist_form_tag_handler', true );
    }
    
    function custom_customlist_form_tag_handler( $tag ) {
    
        $tag = new WPCF7_FormTag( $tag );
    
        if ( empty( $tag->name ) ) {
            return '';
        }
    
        $customlist = '';
    
        $query = new WP_Query(array(
            'post_type' => 'CUSTOM POST TYPE HERE',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'orderby'       => 'title',
            'order'         => 'ASC',
        ));
    
        while ($query->have_posts()) {
            $query->the_post();
            $post_title = get_the_title();
            $customlist .= sprintf( '', 
    esc_html( $post_title ), esc_html( $post_title ) );
        }
    
        wp_reset_query();
    
        $customlist = sprintf(
            '', $tag->name,
        $tag->name . '-options',
            $customlist );
    
        return $customlist;
    }
    

    Then you use the tag in contact form 7 like this.

    [customlist your-field-name]
    

    Hopefully this helps someone else who was looking for a way to do this like I was.

    You could alter it to get any information you need from the custom post type.

    It does not have any validation though.

提交回复
热议问题