Showing posts with label add title to contact form7. Show all posts
Showing posts with label add title to contact form7. Show all posts

Wednesday, March 1, 2017

add post or custom post title in contact form 7

Hello
I am trying to:
load all my post titles of a specific custom post type into a select box of my contact form.
Currently, I'm using Contact Form 7

Here is the solution :
You need to create a custom shortcode in the contact form 7 for the select box. In order to create the custom shortcode for displaying title in select box, you can use the following custom shortcode.

Add this code to your function.php file.

add_action( 'wpcf7_init', 'custom_views_post_title' );
  
function custom_views_post_title() {
    wpcf7_add_shortcode( 'custom_views_post_title', 'custom_views_post_title_shortcode_handler' );
}
  
function custom_views_post_title_shortcode_handler( $tag ) {
    global $post;
    $args = array( 'post_type' => 'event' );
    $myposts = get_posts( $args );
    $output = '<select name="eventname" id="fleet" onchange="document.getElementById(\'fleet\').value=this.value;"><option></option>';
    foreach ( $myposts as $post ) : setup_postdata($post);
       $title = get_the_title();
       $output .= '<option value="'. $title .'">'. $title .' </option>';
    endforeach;
    $output .= "</select>";
    return $output;
}
and use the shortcode [custom_views_post_title] in contact form 7.
Now how to display in contact form 7 email :
Your code is :
$output = '<select name="eventname">
So write  [eventname]