Creating Custom Shortcodes For Contact Form 7

With more than 30,000,000 downloads, contact form 7 is one of the most popular contact form plugins available for WordPress. It allows you to build multiple forms and customize them.

Useful Read : How To Disable Directory Browsing in WordPress?

In this post, I am sharing a snippet which can be used to build a custom contact form 7 shortcode for your form. To create a custom shortcode, we are going to use wpcf7_add_shortcode() function.

//Contact Form Shortcode
wpcf7_add_shortcode('postdropdown', 'vks_createbox', true);
function vks_createbox(){
	global $post;
	$args = array( 'post_type' => 'motorcycle', 'posts_per_page' => -1 );
	$myposts = get_posts( $args );
	$output = "<select name='cursus' id='cursus'><option>--Select a Model--</option>";
	foreach ( $myposts as $post ) : setup_postdata($post);
			$title = get_the_title();
			$output .= "<option value='$title'> $title </option>";
	$output .= "</select>";
	return $output;

The above code will create a shortcode [postdropdown], which generates a select box with custom post type’s (motorcycle’s) title as its options.

Leave a Comment.