Support » Plugin: Contact Form 7 » [Plugin: Contact Form 7] Populate Select Box from database

  • Resolved Katch22



    Im wondering if its possibly to populate a drop down selection box from content stored within the database (in my case being a list of courses)?

    I have the php script to show the list in regular browser, i’ve put together an attempt and creating a function to display the code via shortcode without a lot of success.

    I have even looked at some extensions for Contact Form 7 but found nothing that could do this for a select box.

    Any help/advice would be appreciated, I desperate to get this part completed!


Viewing 13 replies - 1 through 13 (of 13 total)
  • Any ideas?

    I found a solution posted here.

    They populated the dropdown with post titles from a specified category.

    The file you will need to modify is select.php and it is under the “modules” folder with in cf7.
    Open up select.php and on line 93 (right under $label = $value;)
    paste the following code.

    if ($name == 'events') {
     $cf7_dropdown = new WP_Query("cat=1");
     while($cf7_dropdown->have_posts()) : $cf7_dropdown->the_post();  
    	  if ( !in_array( get_the_title(), $do_not_duplicate ) ) {
    		 $do_not_duplicate[] = array_unique($do_not_duplicate);
    	 	 $html .= '<option value="' . esc_attr(get_the_title()) . '"' . '>' . esc_html(get_the_title()) . '</option>';
    		  array_push($do_not_duplicate, get_the_title());
        } else {

    In my case, I had a custom post type (Events), that needed to get a list of all events from this day forward (which is why the query string has the meta_value and meta_compare within it).

    Here is the code I used:

    // custom for events list
    if ($name == 'events') {
     $cf7_dropdown = new WP_Query('post_type=event&numberposts=-1&order=ASC&meta_key=event_date&meta_compare=>=&meta_value=' . date("m/d/Y") . '&orderby=meta_value_num');
     while($cf7_dropdown->have_posts()) : $cf7_dropdown->the_post();
      $custom = get_post_custom($post->ID);
    	 	  $html .= '<option value="' . esc_attr(get_the_title()) . ' - ' . $custom["event_date"][0] . '"' . '>' . esc_html(get_the_title()) . ' - ' . $custom["event_date"][0] . '</option>';
        } else { //end custom for events list

    Do note that with this, you’ll need to add a another closing bracket to the very end of the file – changing
    <?php } ?>

    <?php }
    } ?>

    Let me know if you’ve got any questions…

    I can offer a better solution, or at least one that does not require hacking the contact form 7 plugin..

    I’m not sure if this is even the best way, but it works for me!

    I added the plugin:

    and then in the form where I want the select box to be, I add some shortcode..

    [autoselectfleet][dynamichidden fleet id:fleet ]

    The page is using a template,

    `$content = do_shortcode( $post->post_content );
    $new_content = str_replace(‘[autoselectfleet]’, $fleet_selector, $content);`

    where $fleet_selector is the selector I made..

    On that select box,


    and I also added a javascript snippet to first populate that box on page load with a preselected value for the dropdown, as they arrive on my form after first picking something somewhere else, and that value is one of the dropdown values, I added select=’selected’ to the value in the dropdown as well..

    all this simply allowed the email to grab those dynamic hidden values and use them as needed..

    Hope to have helped, lemme know if you need better explanation..

    The Todd


    I like your solution Todd but I’m not sure I completely understand the logic of how it’s implemented.

    Should I be building the select box HTML in the page template and then replacing the shortcode with it?

    In the end I used a slightly different solution based on the Contact Form 7 Dynamic Text Extension.

    I didn’t use any JavaScript and I think this is a less hacky way to do it. From my themes functions.php:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    thank you todd, i try to implement your method.
    i get this php error: “Cannot redeclare createbox()”
    My code is: ( i use a plugin “Shortcode Exec PHP” )

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    thank you todd, i try to implement your method.
    i get this php error: “Cannot redeclare createbox()”
    My code is: ( i use a plugin “Shortcode Exec PHP” )

    Anyone who can help us further on this subject? I am also getting an error Todd, what exact code you used?


    Hi guys,

    This is the code that makes it all work. Maybe not the best script? but it does really work. Simple add this function in functions.php:

    wpcf7_add_shortcode('postdropdown', 'createbox');
    function createbox(){ 
    global $post;
    $args = array( 'category' => 5 );
    $myposts = get_posts( $args );
    $output = '<select name="lstdate" id="fleet" onchange="document.getElementById(\'fleet\').value=this.value;"><option></option>';
    foreach ( $myposts as $post ) : setup_postdata($post);
    	$title = get_the_title();
    	$output .= "<option> $title </option>";
    $output .= "</select>";
    return $output;	

    With the shortcode in contactform 7 [postdropdown]. This will show all the posts titles in category 5.


    To fully get it functioning, use this updated code:

    Updated code:

    wpcf7_add_shortcode('postdropdown', 'createbox', true);
    function createbox(){
    global $post;
    $args = array('numberposts' => 0, 'category' => 5 );
    $myposts = get_posts( $args );
    $output = "<select name='cursus' id='cursus' onchange='document.getElementById(\"cursus\").value=this.value;'><option></option>";
    foreach ( $myposts as $post ) : setup_postdata($post);
    	$title = get_the_title();
    	$output .= "<option value='$title'> $title </option>";
    $output .= "</select>";
    return $output;

    With the shortcode [postdropdown cursus] in the contactform to show posts from category 5. Put shortcode [cursus] in the mailtemplate.


    Hi – how about populating the registration fields from an existing contact data base. The user has already logged in with name + email before they get to the registration screen.

    Has any one already done this?

    Thanks in avance!


    Hey WZShop. Thanks this works great…

    Do you know how to adjust this code to get multiple selectors from different catergories?


    Do you have any ideas how to get this work with custom post types instead of category ?

    I’ve tried this (and that…) but nothing shows up in the drop-down list:

    $args = array( 'numberposts' => 0, 'post_type' => 'offre');


Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘[Plugin: Contact Form 7] Populate Select Box from database’ is closed to new replies.