I copied the filter from the post directly into my functions and manually created an array to test. The result is that only the first letter of the label is displayed, even though the options array is properly created. Embed for image seems to not be wokring. Here is the link to the image: https://ibb.co/6Pj3XKZ
{
"1": "one",
"2": "two",
"3": "three"
}
add_filter( 'cf7sg_custom_dynamic_select','student_name_dynamic_options',10,3);
/**
* Filter dropdown options for dynamic drodpwn list of taxonomy terms.
* @param Array $options the option to filter.
* @param WPCF7_FormTag $tag field tag object.
* @param string $cf7_key the form unique key.
* @return Array $options return either an array of <option value>=><option label> pairs or 2 arrays, one for values and another for attributes.
*/
function student_name_dynamic_options($options, $tag, $cf7_key){
if('ipad-incident-report'!==$cf7_key || 'student_name' !== $tag->name){
return $options;
}
//these are the label users will see when the dropdown opens.
//you can group your options if need be. Let's assume you have an array of arrays of data to display in groups.
//$data = ... //fetch your data, either from the database or some other source.
$data = array(
'1' => 'one',
'2' => 'two',
'3' => 'three'
);
foreach($data as $value=>$label){
$options[$value]=$label;
//if you are displaying more complex select2 fields, or imagegrid for dynamic checkbox, then add extra parameters into a 2nd array of attributes,
//$options['values'][$value]=$label;
//$options['attributes'][$values]= array('data-thumbnail'=>'<image url>');
}
return $options;
}
]]>
I’m getting a bit lost with this and my code is probably completely wrong but I have the following
Add this to Contact Form 7:
[dynamic_select dynamic_select-815 "source:filter"]
And then this in my functions.php file, where:
artist_video – ACF Repeater field name
artist_video_title – ACF Sub field name (text type)
artist_video_id – ACF Sub field name (text type)
All that is happening on output is:
<select id="" name="dynamic_select-815" class="wpcf7-form-control cf7sg-dynamic-dropdown wpcf7-dynamic_select">
<option value="choices">Array</option>
</select>
add_filter( 'cf7sg_dynamic_dropdown_custom_options','dynamic_select_815_dynamic_options',10,3);
function dynamic_select_815_dynamic_options($options, $name, $cf7_key){
$data['choices'] = array();
if( have_rows('artist_video') ) {
// while has rows
while( have_rows('artist_video') ) {
// instantiate row
the_row();
// vars
$value = get_sub_field('artist_video_title');
$label = get_sub_field('artist_video_id');
// append to choices
$options['choices'][ $value ] = $label;
}
}
foreach($data as $label=>$value){
$options['choices'][ $value ] = $label;
}
return $options;
}
Any ideas or help would be much appreciated.
]]>Hi,
I want to display dynamic drop down by fetching values from database. In my functions.php file I have written below code :
function cf7_dynamic_select_area(){
global $wpdb;
$results = $wpdb->get_results(“Select * from city_areas”);
if(!empty($results)){
echo “<select name=’id’>”;
foreach($results as $result){
$id = $result->city_id;
$name = $result->city_name;
echo ‘<option value=”‘.$id.'”>’.$name.'</option>’;
}
echo “</select>”;
}
}
add_filter(‘city-filter’, ‘cf7_dynamic_select_city’, 10, 2);
and in contact form using this short code : City : [dynamicselect* city id:city “city-filter”]
So the problem is drop down is coming out of the form in <div class=”post-content”> instead of inside form. How to solve this issue?
Contact form includes
<label> Document (if applicable)
[dynamicselect document id:document "wpcf7_dynamic_select_example1"]</label>
functions.php in child theme includes
function cf7_dynamic_select_do_example1($choices, $args=array()) {
// this function returns an array of
// label => value pairs to be used in
// a the select field
$choices = array(
'-- Make a Selection --' => '',
'Choice 1' => 'Choice 1',
'Choice 2' => 'Choice 2',
'Choice 3' => 'Choice 3',
'Choice 4' => 'Choice 4',
'Choice 5' => 'Choice 5'
);
return $choices;
} // end function cf7_dynamic_select_do_example1
add_filter('wpcf7_dynamic_select_example1',
'cf7_dynamic_select_do_example1', 10, 2);
Label displays but no dropdown. Any ideas please.
]]>Hi,
is there any kind of way to be able to create custom validation for a dynamic select field created by your plugin? A way to make this field mandatory?
Thanks.
Regards,
Holger
as from the title I’m trying to get my meta keys to insert into my contact form 7 in the form of <select>
` // Dynamic Select for Contact Form 7
function dynamic_select_for_custom_posts($choices, $args=array()) {
// Here we grab the posts using the arguments originated from the shortcode
$get_custom_posts = get_posts($args);
// If we have posts, proceed
if ($get_custom_posts) {
// Foreach found custom post, we build the option using the [key] => [value] fashion
foreach ($get_custom_posts as $custom_post) {
$choices[$custom_post->post_title] = $custom_post->post_title;
}
// If we don’t have posts, halt! Lets use a generic not found option
} else {
// Just a generic option to inform nothing was found
$choices[‘No posts found’] = ‘No posts found’;
}
return $choices;
}
// Lets add a suggestive name to our filter (we will use it on the shortcode)
add_filter(‘conjure-posts-dynamically’, ‘dynamic_select_for_custom_posts’, 10, 2);`
after:
[dynamicselect* desired-game class:browser-default class:custom-select include_blank "conjure-posts-dynamically post_type=suite posts_per_page=-1 meta_key=metakey_AMC_tavoli "]
obviously my meta key to recover is the following as you can read:metakey_AMC_tavoli
I just can’t get the function out to get get_post_meta
into function:
$choices[$custom_post->post_title] = $custom_post->post_title;
I don’t want the post titles, but the meta_value of the meta key:metakey_AMC_tavoli
my custom post type: suite
my meta_key: metakey_AMC_tavoli
i can do this?
]]>I am trying to create a property booking form for a website. I use Contact Form 7 and Advanced Custom Field on the website. I need to create a dropdown menu which will have different number range based on the value from a custom field (created with advanced custom fields). So there is a condition between the dropdown menu and the the custom field value.
The drop down menu is a number range, it represents the number of guests, so it is different for each property. I have created a custom field “guests” which is a simple number field and I want the drop down menu in Contact Form 7 to have different values based on this field.
The Contact Form 7 drop down menu requires to enter one value per line, so each value on each line is one option in the dropdown menu.
In php it would be something like this:
<?php
$value = get_field( "property_number_of_guests" );
for ($i = 1; $i <= $value; $i++) {
echo "<option value=' " . $i . " '>" . $i . "</option>" ;
echo "<br/>";
}
?>
Is there any way to create the drop down menu in CF7 with the above functionality, using your plugin?
]]>Hi
I am trying to get dynamic select values using SQL query, i get my values but the problem m facing is I cannot receive the dynamic select box value into email. For example : I’m trying to populate school names from the database to the dynamic select box, but when I submit the form School name doesn’t show the value I select at the time time of form submission.
]]>Hi,
I have a post type “products”, with taxonomy “colors” and terms “black, yellow, blue”.
Product one as the terms “black and yelloW”
Product two as the terms “yellow and blue”
Is it possible to have a drop down menu with the terms corresponding to each product?
Thanks
]]>Hello!
Is there anyway to insert a blank item as the first option, as with “include_blank” on a vanilla Contact Form 7 drop-down menu?
Thank you for your time.
]]>Hi there,
I am wondering, how can I write a filter, that handles this function:
<?php $ajanlatok = new WP_Query('category_name=ajanlatok&posts_per_page=100&orderby=date&order=DESC'); ?>
<?php while ($ajanlatok->have_posts()) : $ajanlatok->the_post(); ?>
<option value="<?php the_ID(); ?>" <?php if ($_GET['offer-id'] == get_the_ID()) { echo("selected"); } ?>><?php the_title(); ?></option>
<?php endwhile; ?>
]]>
Hello,
I would like to have a custom text as a first item and it would be the placeholder. This way the field would not have a selected option by default and it would have a placeholder.
On a standard CF7 dropdown/select field we can achieve this with a “first_as_label” parameter. How could I do this with your plugin?
Thank you.
]]>Hey, guys!
This is one of the most awesome and important plugin extension for Contact Form 7 out there! It is simply very, very great and, if used correctly, can bring a lot of freedom on our contact forms! But… as some other people I noticed, I had a hard time to figure it out a correct and simple usage of the whole thing. So, I decided to help!
The scenario:
Let’s assume you want to add, on your contact form, a dynamic select/option presenting a choice of products, where your products are a custom post called “product” (I am using products just for example, it could be any custom post, or even “page” ou regular “post” as well).
STEP 1: On your functions.php
Add the following function/filter:
// Dynamic Select for Contact Form 7
function dynamic_select_for_custom_posts($choices, $args=array()) {
// Here we grab the posts using the arguments originated from the shortcode
$get_custom_posts = get_posts($args);
// If we have posts, proceed
if ($get_custom_posts) {
// Foreach found custom post, we build the option using the [key] => [value] fashion
foreach ($get_custom_posts as $custom_post) {
$choices[$custom_post->post_title] = $custom_post->post_title;
}
// If we don't have posts, halt! Lets use a generic not found option
} else {
// Just a generic option to inform nothing was found
$choices['No posts found'] = 'No posts found';
}
return $choices;
}
// Lets add a suggestive name to our filter (we will use it on the shortcode)
add_filter('conjure-posts-dynamically', 'dynamic_select_for_custom_posts', 10, 2);
As you can see above, inside the function we don’t mention any kind of argument related to custom posts, to post sorting, to number of posts and so on. We will provide this right away through the shortcode, written inside the CF7 form.
STEP 2: Inside the CF7 form:
To make use of the function created above, you will need to add the following shortcode to your CF7 form:
[dynamicselect* desired-product "conjure-posts-dynamically post_type=product posts_per_page=-1 orderby=title order=asc"]
It’s pretty simple! Our filter, created on STEP 1 has the name of “conjure-posts-dynamically”, so, we just call it inside the shortcode followed by traditional query_posts parameters as arguments.
Important: the filter name AND ITS arguments must be enclosed inside double quotes, all together.
I used the name “desired-product” in the select/option combo just to make things more easy to understand, but, of course, you should rename it to anything you want. Just to refresh the memory — the field name is the parameter that you set up inside the mail template, in the Email TAB (second tab) of the CF7 form builder.
Other examples using different arguments:
Regular post: Let’s grab only news categorized as videogame news limiting only to the last 10 entries:
[dynamicselect* desired-news "conjure-posts-dynamically post_type=post posts_per_page=10 orderby=date order=desc category=videogame"]
Taxonomy/term & Custom Post: Let’s grab only jazz records from our record collection:
[dynamicselect* desired-record "conjure-posts-dynamically post_type=record posts_per_page=-1 taxonomy=genre term=jazz"]
Meta Values & Custom Post: Let’s grab only Famicom games, from Nintendo:
[dynamicselect* desired-game "conjure-posts-dynamically post_type=game posts_per_page=-1 meta_key=nintendo meta_value=famicom"]
That’s it, guys.
I really hope to have been helpful.
For the plugin author — THANK YOU SO MUCH for this plugin.
Feel free to add my post to the documentation if you find it out suitable.
The instructions for how to use this plugin are clearly written for coders. I’ve searched for simple instructions but without luck.
I want to create two dropdowns where the options in the second DD are based on the selection in the first. For example:
Drop down 1 called “Category” has these options:
My account
My orders
Dropdown 2 called “Subject” when ‘My account’ is selected:
Change my password
Close my account
Dropdown 2 called “Subject” when ‘My orders’ is selected:
Delivery status
Returns
Then whatever is selected in DD 2 should appear as the email subject.
Before any selection is made, DD 1 should say “Select a category:” and DD 2 should say “Select a category first.” if no category has been selected and, if a category has been selected it should say “Select a Subject:”
Here is a working example of what I need https://www.thenorthface.co.uk/help/contact-us.html
I think providing simple step by step instructions for how to set up something like this would help many people.
I hope you can help.
Many thanks!
]]>Hello.
I need to fill a select with the result of a SQL Query, and I don’t know how. Can you help me?
Here is my code:
function cf7_dynamic_select() {
$choices = array(‘– Select a country –‘ => ”);
$query =array($wpdb->get_results(‘SELECT * FROM wp_custom_countries’));
$choices = array_merge($choices, $query);
return $choices;
}
add_filter(‘my-filter’, ‘cf7_dynamic_select’, 10, 2);
wp_custom_countries has de following structure:
id iso nombre
1 AF Afganistán
2 AX Islas Gland
3 AL Albania
4 DE Alemania
5 AD Andorra
6 AO Angola
7 AI Anguilla
8 AQ Antártida
9 AG Antigua y Barbuda
10 AN Antillas Holandesas
I would like to populate choices with the data
‘nombre1’ => ‘id1’, ‘nombre2’ => ‘id2’, …
Could you help me to do this? I have no good skill in php.
Thanks a lot.
]]>Hi
When user select a option, in email i see only value instead text.
Is it possible to display the text of the selection instead of the value?
Best regards ??
]]>Hello, i’m having problems getting value via email.
This is the CF7 shortcode:
[dynamicselect* dynamicselect-200 "fi-dyn-esecuzione"]
This is the field into mail tab of CF7:
Esecuzione: [dynamicselect-200]
This is the function:
function cf7_dynamic_select_do_example1($choices, $args=array()) {
$choices = array_flip(get_field('prod_esecuzione'));
return $choices;
}
add_filter('fi-dyn-esecuzione', 'cf7_dynamic_select_do_example1', 10, 2);
I see correct values into form dropdown but unfortunately, this is the results in email:
Esecuzione: 0 (instead of selected value)
What’s wrong?
]]>How can I include additional data inside the select option
<select id="select">
<option value="1" data-foo="dogs">this</option>
<option value="2" data-foo="cats">that</option>
<option value="3" data-foo="gerbils">other</option>
</select>
]]>
I have a custom post type named properties
and I want to show all the posts’ titles under the dynamic select. So everytime I add a new property, the select box should have that added as well.
Here’s the argument.
$args = array(
‘post_type’ => ‘properties’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => 3,
);
I tried below code but its not working
wpcf7_dynamic_select post_type=properties
Hi,
I’m trying to add a custom item as last option of my dynamic select but is very difficult for me ??
This is my function:
function cf7_dynamic_select() {
$output = array();
$args = array(
'post_type' => 'page',
'post_parent' => '1331',
'order'=> 'ASC',
'orderby'=> 'menu_order',
'hide_empty' => 0 );
// this is my attemp to add last item
array_push($args, "altro", "altro");
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) :
$the_query->the_post();
$output[get_the_title()] = get_the_title();
endwhile;
return $output;
}
add_filter('my-filter', 'cf7_dynamic_select', 10, 2);
and this is my tag:
[dynamicselect* service id:service "my-filter post_type=page post_parent=1331"]
Thank you!
]]>Hi, i need a quick example on how to get current post custom field. I have a custom field array but when i create my filter i always get key value (1,2,3) instead of array value:
function cf7_dynamic_select_do_example1($choices, $args=array()) {
$choices = get_field('prod_esecuzione');
return $choices;
}
add_filter('fi-dyn-esecuzione', 'cf7_dynamic_select_do_example1', 10, 2);
I always get 0,1,2,3 instead of value
]]>Hello sir, I have two dropdown select fields, where in first field I select the state from the list, If I select “Maharashtra” then other dropdown should automatic load the cities in the selected state. How can i do this?
]]>The plugin’s broken, if you are using the latest version of Contact Form 7.
Explanation and workaround:
$tag (line 40 ff. in the plugin) is not an array anymore, but an object (WPCF7_FormTag).
So line 42 in the plugin should be changed to if (!is_object($tag)) {
in order to work properly.
In addition, all $tag['attribute']
have to be changed to $tag->attribute
.
Hello, I installed and activate the plugin. How can use it ? I didn’t understand the instructions.
Do I have to edit the file contact-form-7-dynamic-select-extension/cf7-dynamic-select-examples.php ?
Thanks
]]>I want to add a dynamic dropdown select list of site users without hard coding the entries. Do you have any examples or tutorials on how to do this? Thank you!
]]>Hello,
I have update my site to latest version of wordpress and plugins.
“Dynamic Select for Contact Form 7” has broke to site, this is the error message:
Fatal error: Call to undefined function wpcf7_add_form_tag() in public_html/wp-content/plugins/contact-form-7-dynamic-select-extension/cf7-dynamic-select.php on line 29
I have delete the filter from functions.php, but the error persist.
Can you help me, please?
Thanks
Hi,
First of all thank you for publishing wonderful add-on of Contact Form 7.
I followed your approach and achieved quite a lot but stuck with one thing.
I have a select box of custom post type and I am populating it by using contact form 7 hook and It list all of its option names & values.
Now I have another select box of custom taxonomy which will show options on the selection of custom post type value.
I am not sure which hook of contact form 7 I will use to populate the options of custom taxonomy.
This is my code for your reference.
add_filter( 'wpcf7_form_tag', array ( $this, 'ses_add_nmn_country_of_nomination_to_contact_form' ), 10, 2);
add_filter( 'wpcf7_form_tag', array ( $this, 'ses_add_nmn_award_category_nomination_to_contact_form' ), 10, 2);
public function ses_add_nmn_country_of_nomination_to_contact_form( $tag, $unused )
{
if ( $tag['name'] != 'nmn_country_of_nomination' )
return $tag;
$args = array ( 'post_type' => 'idcdx_country',
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC' );
$country_list = get_posts($args);
if ( !$country_list )
return $tag;
foreach ( $country_list as $country ) {
$tag['raw_values'][] = $country->post_title;
$tag['values'][] = $country->ID;
$tag['labels'][] = $country->post_title;
}
return $tag;
}
public function ses_add_nmn_award_category_nomination_to_contact_form( $tag, $unused )
{
if ( $tag['name'] != 'nmn_award_category_nomination' )
return $tag;
$terms = get_terms(
array(
'taxonomy' => 'idcdx_category',
'hide_empty' => false,
)
);
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$tag['raw_values'][] = $term->name;
$tag['values'][] = $term->term_id;
$tag['labels'][] = $term->name;
}
}
return $tag;
}
Do you mind to help me out sort out this problem.
Thanks in Advance
]]>Hi,
Can someone please help me to create a dropdown select list in Contact Form 7 to select a published Post.
I want to use this in a job application so a specific Job (blog post) can be selected.
I am not a programmer, so if somebody can take this as a job, please let me know.
]]>Hi we are using dynamic select to list post titles in a dropdown. But since it is required field and already selected. How can we add a placeholder or an empty choice, so that there’s nothing selected in it.
Here’s the function code:
function cf7_dynamic_select() {
$args = array ( ‘post_type’ => ‘post’,
‘numberposts’ => 5000,
‘category’=> 85,
‘orderby’ => ‘title’,
‘order’ => ‘ASC’ );
$label=” “;
$value=”None”;
$option[$label] = $value;
$posts = get_posts($args);
foreach ( $posts as $post ) {
$label = $post->post_title; // label of menu select option
$value = $post->post_title; // value of menu select option
$options[$label] = $value;
}
return $options;
}
add_filter(‘Intl-programs’, ‘cf7_dynamic_select’, 10, 2);
Form tag:
<div class=”column-half”>Program Interested*[dynamicselect* Program “Intl-programs”]</div>
I hope I might be able to explain. We want nothing selected by default. but it selects the top value of the dropdown. how can we place an empty choice? so that the user have to drop down and select one of the choices.
]]>The plugin works great. I was wondering if it could be added an improvement to include optgroup tag. It should be directly from the $choices variable:
$choices = array(
‘– Make a Selection –‘ => ”,
value_1 => array( ‘Element 1’, ‘optgroup_1’ ),
value_2 => array( ‘Element 2’, ‘optgroup_1’ ),
value_3 => array( ‘Element 3’, ‘optgroup_1’ ),
value_4 => array( ‘Element 4’, ‘optgroup_2’ ),
value_5 => array( ‘Element 5’, ‘optgroup_2’ ),
value_6 => array( ‘Element 6’, ‘optgroup_3’ ),
value_7 => array( ‘Element 7’, ‘optgroup_3’ ),
);
This could also lead to improve the dropdown adding the possibility of more customisation for each element:
value => array( ‘Label’, ‘optgroup’, ‘class string’, ‘id’, ‘javascript’ ),
Thanks in advance for your great plugin.
]]>