Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active March 27, 2024 00:10
Show Gist options
  • Save morgyface/56474f0a37abb7d622880daf6eff6e40 to your computer and use it in GitHub Desktop.
Save morgyface/56474f0a37abb7d622880daf6eff6e40 to your computer and use it in GitHub Desktop.
WordPress | Contact Form 7 | Custom select from post list
<?php
// Custom contact form 7 retreat select
add_action( 'wpcf7_init', 'custom_retreat_select' );
function custom_retreat_select() {
wpcf7_add_form_tag( 'retreat_select', 'custom_retreat_handler', array( 'name-attr' => true ) );
}
function custom_retreat_handler( $tag ) {
$atts = array();
$atts['name'] = $tag->name;
$atts['class'] = $tag->get_class_option();
$atts['id'] = $tag->get_id_option();
$atts = wpcf7_format_atts( $atts );
$html = '<select ' . $atts . '>';
$args = array(
'post_type' => 'retreats',
'posts_per_page' => -1,
);
$retreats = get_posts( $args );
foreach ( $retreats as $retreat ):
$retreat_id = $retreat->ID;
$slug = $retreat->post_name;
$title = get_the_title($retreat_id);
$html .= '<option value="' . $slug . '">' . $title . '</option>';
endforeach;
$html .= '</select>';
return $html;
}
@morgyface
Copy link
Author

morgyface commented Jul 1, 2016

Dynamic Select Lists in Contact Form 7 (updated October 2018)

You'd add the above code into your themes functions.php file, then, in this instance, you'd use the tag [retreat_select] when creating your post within the CF7 interface in the dashboard. Here we're creating a select list of custom posts called retreats, it could be anything, just adapt the get_posts $args to return the results you need.

You can also add classes and an ID to the field in the usual CF7 way. So, I ended up using:
[retreat_select retreat class:form-control id:retreat_select].

Then for the mail-tag I used the name; [retreat].

@justinasweb
Copy link

Hello,

How can I make this select field required? The standard way [postlist*] does not work.

Also how do I display the selected value in the email message? The same way with [postlist] does not work.

@rtpHarry
Copy link

rtpHarry commented Jan 26, 2018

Thank for this. FYI the method is deprecated now but its a simple method name change to get it compliant with the current version of cf7:

https://contactform7.com/2015/01/10/adding-a-custom-form-tag/

@techmind86
Copy link

techmind86 commented Aug 18, 2018

Hi,

Thanks for this nice code..
Can this be implemented for multiple select using select2???

Thanks in advance

@zeroazulagencia
Copy link

Hello any way to show the field in the message (email) sent?

@KoolPal
Copy link

KoolPal commented Sep 18, 2018

Hi,

Thanks for this nice code..
Can this be implemented for multiple select using select2???

Thanks in advance

I landed here searching for select2 as well.

Any guidance would be great!

@morgyface
Copy link
Author

Thank for this. FYI the method is deprecated now but its a simple method name change to get it compliant with the current version of cf7:

https://contactform7.com/2015/01/10/adding-a-custom-form-tag/

Finally fixed this @rtpHarry thanks for the tips!

@morgyface
Copy link
Author

Hello any way to show the field in the message (email) sent?

yes @zeroazulagencia, this gist now supports the name field. Give your tag a name and then use the name as the mail tag.

So [retreat_select retreat ] in the form, and then [retreat] in the mail.

@dpasserone
Copy link

Hey morgyface,

Just want to thank you very much because your solution helped me a lot to solve a problem of connection between TablePress and ContactForm7. Thanks to your example and to some string concatenation in php I was able to solve it, and now I am happy :-)
Regards,
Daniele

@wesellhost
Copy link

Hello Morgyface

And your code save me lots of time but I have problem here I used wpml and n list it show me the posts in 2 language is there any way to fix this
thank you

@Halleluyahsalako
Copy link

Hey morgyface,

Just want to thank you very much because your solution helped me a lot to solve a problem of connection between TablePress and ContactForm7. Thanks to your example and to some string concatenation in php I was able to solve it, and now I am happy :-)
Regards,
Daniele

Hi. I am also working with TablePress and Contact form 7 on a website. I want to make a form with contact form 7 with about 3 different select options. The first select option is quite easy to create, but, I want to populate options for the second select option from whichever answer they select on the first dropdown option on the form and then the answer on the second dropdown select option will determine what will populate the third select option, could you be kind enough to help on how to achieve this?

Here is an illustration of what I am trying to say:

First Select Option:
Question: YOU ARE ON WHAT CONTINENT?
Option 1: ASIA
Option 2: AFRICA
Option 3: SOUTH AMERICA.....

Second Select Option:
Question: YOU ARE IN WHAT COUNTRY?
OPTIONS WILL BE POPULATED BASED ON CONTINENT THAT WAS SELECTED ABOVE; For example, I selected AFRICA above, then the select option for countries will be based on countries in Africa... e.g: Nigeria, Ghana... etc

Third Select Option:
Question: YOU RESIDE IN WHAT STATE?
OPTIONS WILL BE POPULATED BASED ON COUNTRY SELECTED IN SELECT OPTION 2 (It will list the states in the country selected)

@morgyface
Copy link
Author

Hey @hall

Hey morgyface,
Just want to thank you very much because your solution helped me a lot to solve a problem of connection between TablePress and ContactForm7. Thanks to your example and to some string concatenation in php I was able to solve it, and now I am happy :-)
Regards,
Daniele

Hi. I am also working with TablePress and Contact form 7 on a website. I want to make a form with contact form 7 with about 3 different select options. The first select option is quite easy to create, but, I want to populate options for the second select option from whichever answer they select on the first dropdown option on the form and then the answer on the second dropdown select option will determine what will populate the third select option, could you be kind enough to help on how to achieve this?

Here is an illustration of what I am trying to say:

First Select Option:
Question: YOU ARE ON WHAT CONTINENT?
Option 1: ASIA
Option 2: AFRICA
Option 3: SOUTH AMERICA.....

Second Select Option:
Question: YOU ARE IN WHAT COUNTRY?
OPTIONS WILL BE POPULATED BASED ON CONTINENT THAT WAS SELECTED ABOVE; For example, I selected AFRICA above, then the select option for countries will be based on countries in Africa... e.g: Nigeria, Ghana... etc

Third Select Option:
Question: YOU RESIDE IN WHAT STATE?
OPTIONS WILL BE POPULATED BASED ON COUNTRY SELECTED IN SELECT OPTION 2 (It will list the states in the country selected)

Hey there, it sounds to me like you need to be using JavaScript for this. Have a look at something like this: https://stackoverflow.com/questions/30232146/dynamically-populating-drop-down-list-from-selection-of-another-drop-down-value
👍🏼

@Halleluyahsalako
Copy link

Halleluyahsalako commented May 8, 2021

@morgyface

Hey there, it sounds to me like you need to be using JavaScript for this. Have a look at something like this: https://stackoverflow.com/questions/30232146/dynamically-populating-drop-down-list-from-selection-of-another-drop-down-value
👍🏼

Thank you so much, I got a fix that works perfectly here: https://bdwm.be/how-to-create-dynamically-populated-cascading-dropdown-lists-for-contact-form-7/

I will however suggest, for future reference to anyone, to check the comments, there were some mistakes in the code that were corrected in the comment session.

Again, thanks for your help and the timely response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment