I have been working on this function for several days and I cannot seem to get the results to display in the dropdown menu I created in contact form 7. I could use some help.
The form has the following shortcode
[select* city_dropdown]
The code is able to create a $tag array that is passed to the CF7 [select]. My understanding is that the 'raw_values' or 'values' fields of this array should be displayed in the dropdown menu.
Array ( [type] => select* [basetype] => select [raw_name] => city_dropdown [name] => city_dropdown [options] => Array ( ) [raw_values] => Array ( ) [values] => Array ( ) [pipes] => WPCF7_Pipes Object ( [pipes:WPCF7_Pipes:private] => Array ( ) ) [labels] => Array ( ) [attr] => [content] =>
[0] => Array ( [type] => select* [basetype] => select [raw_name] => city_dropdown [name] => city_dropdown [options] => Array ( ) [raw_values] => Burnaby,BC [values] => Burnaby,BC [pipes] => WPCF7_Pipes Object ( [pipes:WPCF7_Pipes:private] => Array ( ) ) [labels] => Array ( ) [attr] => [content] => )
[1] => Array ( [type] => select* [basetype] => select [raw_name] => city_dropdown [name] => city_dropdown [options] => Array ( ) [raw_values] => Kelowna,BC [values] => Kelowna,BC [pipes] => WPCF7_Pipes Object ( [pipes:WPCF7_Pipes:private] => Array ( ) ) [labels] => Array ( ) [attr] => [content] => )
[2] => Array ( [type] => select* [basetype] => select [raw_name] => city_dropdown [name] => city_dropdown [options] => Array ( ) [raw_values] => Calgary,AB [values] => Calgary,AB [pipes] => WPCF7_Pipes Object ( [pipes:WPCF7_Pipes:private] => Array ( ) ) [labels] => Array ( ) [attr] => [content] => )
[3] => Array ( [type] => select* [basetype] => select [raw_name] => city_dropdown [name] => city_dropdown [options] => Array ( ) [raw_values] => Calgary,AB [values] => Calgary,AB [pipes] => WPCF7_Pipes Object ( [pipes:WPCF7_Pipes:private] => Array ( ) ) [labels] => Array ( ) [attr] => [content] => )
[4] => Array ( [type] => select* [basetype] => select [raw_name] => city_dropdown [name] => city_dropdown [options] => Array ( ) [raw_values] => Burnaby,BC [values] => Burnaby,BC [pipes] => WPCF7_Pipes Object ( [pipes:WPCF7_Pipes:private] => Array ( ) ) [labels] => Array ( ) [attr] => [content] => ) )
However, the list of cities that is in the raw_values and values fields are not being displayed in the dropdown menu. Am I putting the value in the right spot?
Here is my code:
add_filter( 'wpcf7_form_tag', 'filter_wpcf7_form_tag_select_city_list', 10, 2);
function filter_wpcf7_form_tag_select_city_list($tag, $unused) {
if ( $tag['name'] != 'city_dropdown') {
return $tag;
}
global $wpdb;
$pipes = new WPCF7_Pipes($tag['raw_values']);
$table_name = $wpdb->prefix . 'pi_counter';
$count_query = "select count(*) from $table_name";
$num = $wpdb->get_var($count_query);
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
$arg = $tag;
if($retrieve_data) {
foreach ($retrieve_data as $retrieved_data) {
$seats= $retrieved_data->Count_value;
if($seats != '0'){
$arg['raw_values'] = $retrieved_data->City;
$arg['values'] = $retrieved_data->City;
$arg['pipes'] = $pipes;
array_push($tag, $arg);
}
}
}
else {
echo 'no results or error<br>';
echo 'error: ' . $wpdb->print_error();
}
// $tag = array_unique($tag,$tag['values']);
// echo '</br></br>';
// print_r($tag);
return $tag;
}
Bug #1
The list of cities is supposed to appear in the drop-down select field. Here is a link to the result I see now. The drop-down field is under the label "Event Location'
https://bk.pinterec.ca/2021/11/25/wereg/
Any idea why the list of cities is not displayed?
It is supposed to look like this:
Burnaby,BC
Kelowna,BC
Calgary,AB
Calgary,AB
Burnaby,BC
Bug #2
There is a nagging bug in the code....for some reason, the code runs twice. when I do a print_r($tag) I see the array displayed twice. Minor issue but if you see why...please point it out.
Why does the function run twice?
Bug # 3
For some reason, the array_unique is not filtering out the duplicates.
$tag = array_unique($tag,$tag['values']); // Fourth line from the bottom
Any idea why?
My Answer:
> 'select' [name] => 'city_dropdown' [raw_values] => Array ( [0] => 'New York' [1] => 'Los Angeles' [2] => 'Chicago' ) [values] => Array ( [0] => 'New York' [1] => 'Los Angeles' [2] => 'Chicago' ) )__However, when I view the form on the front end, the dropdown menu is empty. I have tried various combinations of the 'raw_values' and 'values' fields, but nothing seems to work. Can someone please help me figure out what I am doing wrong?
Rate this post
3 of 5 based on 7577 votesComments