certainlyakey
6/23/2014 - 12:55 PM

Adding select element dynamically populated with custom posts to Contact Form 7 WP plugin

Adding select element dynamically populated with custom posts to Contact Form 7 WP plugin

<?php
//Adding select element dynamically populated with custom posts to Contact Form 7 WP plugin
//In CF7 form use [customselect coursesdropdown] format to insert the select element, in the email body - [coursesdropdown]
wpcf7_add_shortcode('customselect', 'createdynamicselect', true);
function createdynamicselect(){
	//Settings
	$selectname = 'coursesdropdown';
	$is_multiple = true;

	global $post;
	$courses = get_posts( array('numberposts'=>-1,'post_type'=>'course', 'orderby'=>'menu_order') );
	$output = "\r\n<select name='".$selectname.( $is_multiple ? "[]" : "" )."' id='".$selectname."' ".( $is_multiple ? "multiple" : "" ).">\r\n";
	foreach ( $courses as $post ) : setup_postdata($post);
	    $coursename = get_the_title();
	    $output .= "<option value='".$coursename."'>".$coursename."</option>\r\n";
	endforeach;
	$output .= "</select>";
	$output = '<span class="wpcf7-form-control-wrap '.$selectname.'">'.$output.'</span>';
	return $output;
}
?>