Contact Form 7 is a great, plugin that allows you to easily add contact forms to your WordPress site. Today, I wanted to make a small tweak to one of my contact forms so I could capture some better information.
Specifically, I often receive blog enquiries from people asking questions like “Can this plugin do XXX?” without any context. Since I have around 13 free plugins on WordPress.org, and around 14 sold through my own premium plugin site, and numerous others through other websites – it’s not always obvious which plugin people are asking about.
So – I did the obvious thing, and added a drop-down list and asked people to confirm which plugin they were contacting me about. The challenge was that rather than just hardcoding in the list of options, I wanted it to be dynamic, and generated in realtime.
I can see this being useful in lots of different situations so here’s my solution:
function ses_add_plugin_list_to_contact_form ( $tag, $unused ) {
if ( $tag['name'] != 'plugin-list' )
return $tag;
$args = array ( 'post_type' => 'wpsc-product',
'numberposts' => 50,
'orderby' => 'title',
'order' => 'ASC' );
$plugins = get_posts($args);
if ( ! $plugins )
return $tag;
foreach ( $plugins as $plugin ) {
$tag['raw_values'][] = $plugin->post_title;
$tag['values'][] = $plugin->post_title;
$tag['labels'][] = $plugin->post_title;
$tag['pipes']->pipes[] = array ( 'before' => $plugin->post_title, 'after' => $plugin->post_title);
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'ses_add_plugin_list_to_contact_form', 10, 2);
That example picks up a select list called “plugin-list”, and adds to the options for every post retrieved by the call to get_posts. Here’s the setup of the default contact form:

You can see we’ve set the field name to “plugin-list” – so that our code above can make the changes to the right field, and we’ve provided a couple of fixed options – our dynamic ones will be added on after these.
Hopefully this comes in useful!
February 4, 2013 at 3:22 pm
Very nice! Just what I needed.
I changed the code a little bit to make it a litte more general:
$options = (array) $tag['options'];
foreach ( $options as $option ) {
if ( preg_match( ‘%^posttype:([-0-9a-zA-Z_]+)$%’, $option, $matches ) ) {
$post_type = $matches[1];
}
}
//check if post_type is set
if(!isset($post_type)) {
return $tag;
}
$args= array(
‘post_type’ => $post_type,
‘order’ => ‘ASC’,
‘orderby’ => ‘title’
);
$plugins = get_posts($args);
This is how I use it:
[select* your-shop posttype:shop]
and it will collect al ‘shop’ posts.