contact form 7 Archives | Lee Willis https://www.leewillis.co.uk/tag/contact-form-7/ Sun, 04 Nov 2012 15:58:27 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 Adding a dynamic select list to Contact Form 7 https://www.leewillis.co.uk/dynamic-select-list-contact-form-7/ https://www.leewillis.co.uk/dynamic-select-list-contact-form-7/#comments Sun, 04 Nov 2012 15:58:27 +0000 http://www.leewillis.co.uk/?p=467 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. … Continue reading

The post Adding a dynamic select list to Contact Form 7 appeared first on Lee Willis.

]]>
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!

The post Adding a dynamic select list to Contact Form 7 appeared first on Lee Willis.

]]>
https://www.leewillis.co.uk/dynamic-select-list-contact-form-7/feed/ 20