Conditional Fields for Contact Form 7 - Version 0.1.2

Version Description

  • Make code work with select element that allows multiple options.
  • Only load javascript on pages that contain a CF7 form
Download this release

Release Info

Developer Jules Colle
Plugin Icon 128x128 Conditional Fields for Contact Form 7
Version 0.1.2
Comparing to
See all releases

Code changes from version 0.1.1 to 0.1.2

contact-form-7-conditional-fields.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Contact Form 7 Conditional Fields
4
  Plugin URI: http://bdwm.be/
5
  Description: Adds support for conditional fields to Contact Form 7. This plugin depends on Contact Form 7.
6
  Author: Jules Colle
7
- Version: 0.1.1
8
  Author URI: http://bdwm.be/
9
  */
10
 
@@ -26,7 +26,7 @@ Author URI: http://bdwm.be/
26
  ?>
27
  <?php
28
 
29
- define( 'WPCF7CF_VERSION', '0.1.1' );
30
  define( 'WPCF7CF_REQUIRED_WP_VERSION', '4.1' );
31
  define( 'WPCF7CF_PLUGIN', __FILE__ );
32
  define( 'WPCF7CF_PLUGIN_BASENAME', plugin_basename( WPCF7CF_PLUGIN ) );
@@ -71,7 +71,7 @@ class ContactForm7ConditionalFields {
71
  }
72
 
73
  public static function enqueue_js() {
74
- wp_enqueue_script('cf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), '', true);
75
  }
76
 
77
  public static function enqueue_css() {
@@ -187,5 +187,6 @@ function wpcf7cf_properties($properties, $wpcf7form) {
187
 
188
  add_action('wpcf7_contact_form', 'wpcf7cf_enqueue_scripts', 10, 1);
189
  function wpcf7cf_enqueue_scripts($cf7form) {
 
190
  wp_localize_script('cf7cf-scripts', 'wpcf7cf_options', get_post_meta($cf7form->id,'wpcf7cf_options',true));
191
  }
4
  Plugin URI: http://bdwm.be/
5
  Description: Adds support for conditional fields to Contact Form 7. This plugin depends on Contact Form 7.
6
  Author: Jules Colle
7
+ Version: 0.1.2
8
  Author URI: http://bdwm.be/
9
  */
10
 
26
  ?>
27
  <?php
28
 
29
+ define( 'WPCF7CF_VERSION', '0.1.2' );
30
  define( 'WPCF7CF_REQUIRED_WP_VERSION', '4.1' );
31
  define( 'WPCF7CF_PLUGIN', __FILE__ );
32
  define( 'WPCF7CF_PLUGIN_BASENAME', plugin_basename( WPCF7CF_PLUGIN ) );
71
  }
72
 
73
  public static function enqueue_js() {
74
+ // nothing here. We will only load the CF7 script if there is a CF7 form on the page.
75
  }
76
 
77
  public static function enqueue_css() {
187
 
188
  add_action('wpcf7_contact_form', 'wpcf7cf_enqueue_scripts', 10, 1);
189
  function wpcf7cf_enqueue_scripts($cf7form) {
190
+ wp_enqueue_script('cf7cf-scripts', plugins_url('js/scripts.js', __FILE__), array('jquery'), '', true);
191
  wp_localize_script('cf7cf-scripts', 'wpcf7cf_options', get_post_meta($cf7form->id,'wpcf7cf_options',true));
192
  }
js/scripts.js CHANGED
@@ -1,31 +1,40 @@
1
  (function($) {
2
- //if (wpcf7cf_options == null) var wpcf7cf_options = [];
3
- console.log(wpcf7cf_options);
4
 
5
  $(document).ready(function() {
6
  function display_fields() {
7
- console.log('display fields');
8
  $('.wpcf7cf_group').hide();
9
  for (var i=0; i < wpcf7cf_options.length; i++) {
10
 
11
  var condition = wpcf7cf_options[i];
12
- console.log(condition);
13
  if (condition.then_visibility == 'hide') continue;
14
 
15
  $field = $('[name='+condition.if_field+']').length ? $('[name='+condition.if_field+']') : $('[name='+condition.if_field+'\\[\\]]');
16
 
17
  if ($field.length == 1) {
18
 
19
- // single field (tested with text field, single checkbox)
 
 
 
 
 
 
 
 
 
 
20
 
21
  if (condition.operator == 'equals' && $field.val() == condition.if_value || condition.operator == 'not equals' && $field.val() != condition.if_value) {
22
  if ($field.attr('type') == 'checkbox' && !$field.attr('checked')) continue;
23
  $('#'+condition.then_field).show();
24
  }
25
 
 
26
  } else if ($field.length > 1) {
27
 
28
- // multiple fields (tested with checkboxes, exclusive checkboxes)
29
 
30
  var all_values = [];
31
  var checked_values = [];
1
  (function($) {
2
+ if (typeof wpcf7cf_options == 'undefined') return; // return if there is no form on the page
3
+ //console.log(wpcf7cf_options);
4
 
5
  $(document).ready(function() {
6
  function display_fields() {
 
7
  $('.wpcf7cf_group').hide();
8
  for (var i=0; i < wpcf7cf_options.length; i++) {
9
 
10
  var condition = wpcf7cf_options[i];
 
11
  if (condition.then_visibility == 'hide') continue;
12
 
13
  $field = $('[name='+condition.if_field+']').length ? $('[name='+condition.if_field+']') : $('[name='+condition.if_field+'\\[\\]]');
14
 
15
  if ($field.length == 1) {
16
 
17
+ // single field (tested with text field, single checkbox, select with single value (dropdown), select with multiple values)
18
+
19
+ if ($field.is('select')) {
20
+ $field.find('option:selected').each(function() {
21
+ var $option = $(this);
22
+ if (condition.operator == 'equals' && $option.val() == condition.if_value) {
23
+ $('#'+condition.then_field).show();
24
+ }
25
+ });
26
+ continue;
27
+ }
28
 
29
  if (condition.operator == 'equals' && $field.val() == condition.if_value || condition.operator == 'not equals' && $field.val() != condition.if_value) {
30
  if ($field.attr('type') == 'checkbox' && !$field.attr('checked')) continue;
31
  $('#'+condition.then_field).show();
32
  }
33
 
34
+
35
  } else if ($field.length > 1) {
36
 
37
+ // multiple fields (tested with checkboxes, exclusive checkboxes, dropdown with multiple values)
38
 
39
  var all_values = [];
40
  var checked_values = [];
readme.txt CHANGED
@@ -5,7 +5,7 @@ Website: http://bdwm.be
5
  Tags: wordpress, contact form 7, forms, conditional fields
6
  Requires at least: 3.6.1
7
  Tested up to: 4.5.2
8
- Stable tag: 0.1.1
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -42,4 +42,9 @@ First release
42
 
43
  = 0.1.1 =
44
 
45
- Fixed bug with exclusive checkboxes (https://wordpress.org/support/topic/groups-not-showing)
 
 
 
 
 
5
  Tags: wordpress, contact form 7, forms, conditional fields
6
  Requires at least: 3.6.1
7
  Tested up to: 4.5.2
8
+ Stable tag: 0.1.2
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
42
 
43
  = 0.1.1 =
44
 
45
+ Fixed bug with exclusive checkboxes (https://wordpress.org/support/topic/groups-not-showing)
46
+
47
+ = 0.1.2 =
48
+
49
+ * Make code work with select element that allows multiple options.
50
+ * Only load javascript on pages that contain a CF7 form