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]

7 comments:

  1. very interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post! Search Bar Firefox 57 Quantum addon

    ReplyDelete
  2. Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.

    python training in chennai
    python course in chennai
    python training in bangalore

    ReplyDelete