├── .gitignore ├── screenshot-1.png ├── cf7-dynamic-select-examples.php ├── README.md ├── readme.txt └── cf7-dynamic-select.php /.gitignore: -------------------------------------------------------------------------------- 1 | _notes 2 | .DS_Store 3 | *.LCK 4 | *.svn 5 | -------------------------------------------------------------------------------- /screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hube2/contact-form-7-dynamic-select-extension/master/screenshot-1.png -------------------------------------------------------------------------------- /cf7-dynamic-select-examples.php: -------------------------------------------------------------------------------- 1 | value pairs to be used in the select field 12 | $choices = array( 13 | '-- Make a Selection --' => '', 14 | 'Choice 1' => 'Choice 1', 15 | 'Choice 2' => 'Choice 2', 16 | 'Choice 3' => 'Choice 3', 17 | 'Choice 4' => 'Choice 4', 18 | 'Choice 5' => 'Choice 5', 19 | 'default' => array('Choice 2', 'Choice 3') 20 | ); 21 | return $choices; 22 | } // end function cf7_dynamic_select_do_example1 23 | add_filter('wpcf7_dynamic_select_example1', 'cf7_dynamic_select_do_example1', 10, 2); 24 | 25 | ?> -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Dynamic Select for Contact Form 7 2 | --------------------------------------- 3 | 4 | # Adopt my Contact Form 7 (CF7) Plugins 5 | 6 | I am putting my 3 CF7 plugins up for adoption. 7 | 8 | The reasons for this are: 9 | * I no longer use CF7 for my clients. 10 | * It is too difficult to use for my average client 11 | * It does not support the features my clients want 12 | * The authour of CF7 makes too many backwards compatibility breaking changes 13 | * I simply do not have time to keep up with maintenence for plugins I no longer use 14 | 15 | These plugins do have users and are usefull for those that use CF7 and I'd rather someone else takes them over than let them die. 16 | 17 | The 3 plugins involved are: 18 | * At WP 19 | * [Dynamic Select for Contact Form 7](https://wordpress.org/plugins/contact-form-7-dynamic-select-extension/) 20 | * [Hidden Field for Contact Form 7](https://wordpress.org/plugins/contact-form-7-simple-hidden-field/) 21 | * [Dynamic Recipient for Contact Form 7](https://wordpress.org/plugins/contact-form-7-dynamic-mail-to/) 22 | * On Github 23 | * [Dynamic Select for Contact Form 7](https://github.com/Hube2/contact-form-7-dynamic-select-extension) 24 | * [Hidden Field for Contact Form 7](https://github.com/Hube2/contact-form-7-simple-hidden-field) 25 | * [Dynamic Recipient for Contact Form 7](https://github.com/Hube2/contact-form-7-dynamic-mail-to) 26 | 27 | These plugins were built to work togther 28 | * The dynamic select to populate drop downs 29 | * The dynamic recipient send emails to different people 30 | * The hidden field to supply the fields that the recipient plugin needs to work 31 | 32 | If you are interested in adopting all 3 of these plugins please open up an issue in any of the Github repos for any of these plugins. Below is how I would like to do this, if you have other ideas you can bring them up when you open the issue. 33 | 34 | * You fork all of these repos 35 | * You maintian, update and improve these plugins as needed an send me pull requests 36 | * You take over supporting these plugins on Github and WP (answer questions etc) 37 | * I will, for a short time continue to update the WP repo with your changes 38 | * At some time in the future I will transfer ownership of the both the Github and WordPress repos to you. 39 | 40 | Add dynamically generated select fields (Drop-down menus) to forms in Contact Form 7 using filters 41 | 42 | How To Use 43 | ---------- 44 | 45 | 1) Create a filter to be called from your CF7 Dynamic Select Field. 46 | 47 | Example Filter: 48 | 49 | ``` 50 | function cf7_dynamic_select_do_example1($choices, $args=array()) { 51 | // this function returns an array of 52 | // label => value pairs to be used in 53 | // a the select field 54 | $choices = array( 55 | '-- Make a Selection --' => '', 56 | 'Choice 1' => 'Choice 1', 57 | 'Choice 2' => 'Choice 2', 58 | 'Choice 3' => 'Choice 3', 59 | 'Choice 4' => 'Choice 4', 60 | 'Choice 5' => 'Choice 5' 61 | ); 62 | return $choices; 63 | } // end function cf7_dynamic_select_do_example1 64 | add_filter('wpcf7_dynamic_select_example1', 'cf7_dynamic_select_do_example1', 10, 2) 65 | ``` 66 | 67 | 2) Enter the filter name and any arguments into the Fitler Field when adding a Dynamic Select Field. 68 | For example, if we need to supply a term_id so that the filter can get the posts in a category the 69 | filter value entered would look something like this: 70 | ``` 71 | my-filter term_id=9 72 | ``` 73 | 74 | ***Do Not Include any extra spaces or quotes arround values, names or the =*** 75 | 76 | You can pass any number are arguments to your filter and they will be converted into an array. For example the 77 | following: 78 | ``` 79 | my-filter product-type=101 brand=500 80 | ``` 81 | This will call the function assocaited with the filter hook 'my-filter' with an arguments the argument array of: 82 | ``` 83 | $args = array( 84 | 'product-type' => 101, 85 | 'brand' => 500 86 | ) 87 | ``` 88 | 89 | Your filter must return an array. The array must be a list of "Label" => "Value" pairs. 90 | For mor information see the example in cf7-dynamic-select-examples.php 91 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Dynamic Select for Contact Form 7 === 2 | Contributors: Hube2 3 | Tags: adopt-me, contact form 7, dynamic, select, drop down menu 4 | Requires at least: 4.0 5 | Tested up to: 4.9 6 | Stable tag: 2.0.3 7 | Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=hube02%40earthlink%2enet&lc=US&item_name=Donation%20for%20WP%20Plugins%20I%20Use&no_note=0&cn=Add%20special%20instructions%20to%20the%20seller%3a&no_shipping=1¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted 8 | License: GPLv2 or later 9 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 10 | 11 | Add Dynamic Select Fields in Contact Form 7. 12 | 13 | 14 | == Description == 15 | 16 | Addopt this plugin. [Please see details on Github](https://github.com/Hube2/contact-form-7-dynamic-select-extension). 17 | 18 | Create dynamic select fields in contact form 7. Requires Contact Form 7. 19 | 20 | Field values of dynamic select field are populated by using filters. 21 | 22 | 23 | How To Use 24 | ---------- 25 | 26 | 1) Create a filter to be called from your CF7 Dynamic Select Field. 27 | 28 | Example Filter: 29 | 30 | `function cf7_dynamic_select_do_example1($choices, $args=array()) { 31 | // this function returns an array of 32 | // label => value pairs to be used in 33 | // a the select field 34 | $choices = array( 35 | '-- Make a Selection --' => '', 36 | 'Choice 1' => 'Choice 1', 37 | 'Choice 2' => 'Choice 2', 38 | 'Choice 3' => 'Choice 3', 39 | 'Choice 4' => 'Choice 4', 40 | 'Choice 5' => 'Choice 5' 41 | ); 42 | return $choices; 43 | } // end function cf7_dynamic_select_do_example1 44 | add_filter('wpcf7_dynamic_select_example1', 45 | 'cf7_dynamic_select_do_example1', 10, 2);` 46 | 47 | 2) Enter the filter name and any arguments into the Filter Field when adding a Dynamic Select Field. 48 | For example, if we need to supply a term_id so that the filter can get the posts in a category the 49 | filter value entered would look something like this: 50 | 51 | `my-filter term_id=9` 52 | 53 | ***Do Not Include any extra spaces or quotes arround values, names or the =*** 54 | 55 | You can pass any number are arguments to your filter and they will be converted into an array. For example the 56 | following: 57 | 58 | `my-filter product-type=101 brand=500` 59 | 60 | This will call the function assocaited with the filter hook 'my-filter' with an arguments the argument array of: 61 | `$args = array( 62 | 'product-type' => 101, 63 | 'brand' => 500 64 | )` 65 | 66 | Your filter must return an array. The array must be a list of "Label" => "Value" pairs. 67 | For more information see the example in cf7-dynamic-select-examples.php included in the plugin folder. 68 | 69 | [Also on GitHub](https://github.com/Hube2/contact-form-7-dynamic-select-extension) 70 | 71 | == Installation == 72 | 73 | 1. Upload the files to the plugin folder of your site 74 | 2. Activate it from the Plugins Page 75 | 76 | 77 | == Screenshots == 78 | 79 | 1. Create Dynamic Select Field 80 | 81 | 82 | == Frequently Asked Questions == 83 | 84 | = Why Filters? = 85 | 86 | Many other plugins of this type use shortcodes. I'm not a real fan of shortcodes, but that's not the only 87 | reason. 88 | 89 | Filters are much more flexible that shortcodes. 90 | 91 | For example, a shortcode cannot return an array. A shortcode pretty much requires that only a text value is returned. 92 | 93 | 94 | == Changelog == 95 | 96 | = 2.0.3 = 97 | * correct fatal error called to undefined function when CF7 in not active. 98 | 99 | = 2.0.2 = 100 | * corrected to work with CF7 >= 4.8 101 | 102 | = 2.0.1 = 103 | * corrected fatal function call error due to update in CF7 104 | 105 | = 2.0.0 = 106 | * Replaced depricated CF7 function calls 107 | 108 | = 1.2.1 = 109 | * corrected possible fatal error from duplicate function 110 | 111 | = 1.2.0 = 112 | * Added default value logic 113 | * Corrected Bug: allow multiple not working when inserting new field 114 | 115 | = 1.1.3 = 116 | * Corrected PHP notice "Indirect modification of overloaded element of WPCF7_Validation has no effect" 117 | 118 | = 1.1.2 = 119 | 120 | * Fix validation error removing other attributes, thanks pjgalbraith 121 | * Changed name of plugin to meet new WP guidelines 122 | 123 | = 1.1.1 = 124 | 125 | * Corrected a bug, incorrect field type name when creating new field, introduced in 1.1.0 126 | 127 | = 1.1.0 = 128 | 129 | * Updated tag pane to be completely compatible w/ CF7 >= V4.2 130 | * Backwards compatible w/ CF7 < V4.2 131 | 132 | = 1.0.2 = 133 | 134 | * updated to work with CF7 V4.2 (tag pane still needs some work to make it 100% but it can be used and it will still work with previous versions of CF7) 135 | 136 | = 1.0.1 = 137 | 138 | * Preserve $_GET value - If the value of the field is present in query string, preset the selected values. 139 | 140 | = 1.0.0 = 141 | 142 | * initial release -------------------------------------------------------------------------------- /cf7-dynamic-select.php: -------------------------------------------------------------------------------- 1 | is_posted()) { 116 | if (isset($_POST['_wpcf7_mail_sent']) && $_POST['_wpcf7_mail_sent']['ok']) { 117 | $value = ''; 118 | } else { 119 | $value = stripslashes_deep($_POST[$name]); 120 | } 121 | } else { 122 | if (isset($_GET[$name])) { 123 | $value = stripslashes_deep($_GET[$name]); 124 | } 125 | } 126 | 127 | $filter = ''; 128 | $filter_args = array(); 129 | $filter_string = ''; 130 | if (isset($values[0])) { 131 | $filter_string = $values[0]; 132 | } 133 | if ($filter_string != '') { 134 | $filter_parts = explode(' ', $filter_string); 135 | $filter = trim($filter_parts[0]); 136 | $count = count($filter_parts); 137 | for ($i=1; $i<$count; $i++) { 138 | if (trim($filter_parts[$i]) != '') { 139 | $arg_parts = explode('=', $filter_parts[$i]); 140 | if (count($arg_parts) == 2) { 141 | $filter_args[trim($arg_parts[0])] = trim($arg_parts[1], ' \''); 142 | } else { 143 | $filter_args[] = trim($arg_parts[0], ' \''); 144 | } 145 | } // end if filter part 146 | } // end for 147 | } // end if filter string 148 | if ($filter == '') { 149 | return $filter; 150 | } 151 | //$field_options = do_shortcode('['.$shortcode.']'); 152 | //echo '
'; print_r($field_options); echo ''; 153 | //echo 'here'; die; 154 | //echo $name.': '.$filter.'
'; print_r($filter_args); echo 'FIELD_OPTIONS:
'; print_r($field_options); echo ''; 159 | 160 | //echo '
'; print_r($field_options); echo ''; 161 | 162 | if (!is_array($field_options) || !count($field_options)) { 163 | // filter did not return an array of values 164 | return ''; 165 | } 166 | 167 | $validation_error = ''; 168 | if (is_a($wpcf7_contact_form, 'WPCF7_ContactForm')) { 169 | $validation_error = $wpcf7_contact_form->validation_error($name); 170 | } 171 | //echo '
'; print_r($wpcf7_contact_form); echo ''; 172 | $invalid = 'false'; 173 | if ($validation_error) { 174 | $invalid = true; 175 | $atts .= ' aria-invalid="'.$invalid.'"'; 176 | } 177 | 178 | $default = ''; 179 | if (isset($field_options['default'])) { 180 | $default = $field_options['default']; 181 | unset($field_options['default']); 182 | } 183 | if (!is_array($default)) { 184 | $default = array($default); 185 | } 186 | if (!$multiple && count($default) > 1) { 187 | $default = array(array_pop($default)); 188 | } 189 | $use_default = true; 190 | if (isset($_POST[$name]) || isset($_GET[$name])) { 191 | $use_default = false; 192 | } 193 | 194 | ob_start(); 195 | ?> 196 | 197 | 220 | 221 | 222 | invalidate($tag_o, wpcf7_get_message('invalid_required')); 257 | //$result['valid'] = false; 258 | //$result['reason'][$name] = $wpcf7_contact_form->message('invalid_required'); 259 | } 260 | return $result; 261 | } // end public function validation_filter 262 | 263 | public function add_tg_generator() 264 | { 265 | // called on init to add the tag generator or cf7 266 | // wpcf7_add_tag_generator($name, $title, $elm_id, $callback, $options = array()) 267 | if (!function_exists('wpcf7_add_tag_generator')) { 268 | return; 269 | } 270 | $name = 'dynamicselect'; 271 | $title = __('Dynamic Select field', 'wpcf7'); 272 | $elm_id = 'wpcf7-tg-pane-dynamicselect'; 273 | $callback = array($this, 'tg_pane'); 274 | wpcf7_add_tag_generator($name, $title, $elm_id, $callback); 275 | } // end public function add_tag_generator 276 | 277 | public function tg_pane($form, $args = '') 278 | { 279 | // output the code for CF7 tag generator 280 | $type='dynamicselect'; 281 | if (class_exists('WPCF7_TagGenerator')) { 282 | // tag generator for CF7 >= v4.2 283 | $args = wp_parse_args($args, array()); 284 | $desc = __('Generate a form-tag for a Dynamic Select field. For more details, see %s.'); 285 | $desc_link = ''.__('Contact Form 7 - Dynamic Select Extension').''; 286 | ?> 287 |