Contact form 7 and custom post type

I want to use contact form 7 in Wordpress to create an order form. I want the contents of the order form to be filled with content from the custom message type "Trade Show Material". The message type contains the fields "name", "number", "description", "photo". The idea is that each part can be selected from the mold. Can anyone suggest a general direction for this? Should I possibly fully use another plugin?

+5
source share
3 answers

Perhaps you can use the wpcf7_form_tag filter hook for this.

If you want to use a personalized message type as the parameters of the drop-down list (select), you can add something like the example below in your .php functions:

 function dynamic_field_values ( $tag, $unused ) { if ( $tag['name'] != 'your-field-name' ) return $tag; $args = array ( 'numberposts' => -1, 'post_type' => 'your-custom-post-type', 'orderby' => 'title', 'order' => 'ASC', ); $custom_posts = get_posts($args); if ( ! $custom_posts ) return $tag; foreach ( $custom_posts as $custom_post ) { $tag['raw_values'][] = $custom_post->post_title; $tag['values'][] = $custom_post->post_title; $tag['labels'][] = $custom_post->post_title; } return $tag; } add_filter( 'wpcf7_form_tag', 'dynamic_field_values', 10, 2); 

In the form you can add a field:

 [select* your-field-name include_blank] 

In the above example, post_title is used in the dropdown options. Here you can add your fields (name, number, description, photo).

+10
source

I do not think that wpcf7_form_tag works the same way as vikent showed in his big answer before. Perhaps it has changed since 2015.

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

Keeping this in mind 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 my own dropdown for the custom message 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( '<option value="%1$s">%2$s</option>', esc_html( $post_title ), esc_html( $post_title ) ); } wp_reset_query(); $customlist = sprintf( '<select name="%1$s" id="%2$s">%3$s</select>', $tag->name, $tag->name . '-options', $customlist ); return $customlist; } 

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

 [customlist your-field-name] 

Hope this helps someone who was looking for a way to do this, like me.

You can change it to get the necessary information from a custom message type.

However, he has no verification.

0
source

Thank's man. It really helped me. Works like a charm;)
I just change post_type to "Seguros", my own post type.
In the form, I add [customlist seguros] and all my posts are there.

-3
source

Source: https://habr.com/ru/post/1210760/


All Articles