Contact Form 7 Multi-Step Forms - Version 1.1

Version Description

renamed all function names to be more consistent. use cookies before falling back to session. added back shortcode so users can go back to previous step.

Download this release

Release Info

Developer webheadllc
Plugin Icon 128x128 Contact Form 7 Multi-Step Forms
Version 1.1
Comparing to
See all releases

Code changes from version 1.02 to 1.1

contact-form-7-multi-step-module.php CHANGED
@@ -5,7 +5,7 @@ Plugin URI: http://www.mymonkeydo.com/contact-form-7-multi-step-module/
5
  Description: Enables the Contact Form 7 plugin to create multi-page, multi-step forms.
6
  Author: Webhead LLC.
7
  Author URI: http://webheadcoder.com
8
- Version: 1.02
9
  */
10
  /* Copyright 2012 Webhead LLC (email: info at webheadcoder.com)
11
 
@@ -29,6 +29,7 @@ if (!in_array('contact-form-7-modules/hidden.php', get_option( 'active_plugins',
29
  require_once(plugin_dir_path(__FILE__) . 'module-hidden.php');
30
  }
31
  require_once(plugin_dir_path(__FILE__) . 'module-session.php');
 
32
 
33
  /**
34
  * init_sessions()
@@ -36,43 +37,122 @@ require_once(plugin_dir_path(__FILE__) . 'module-session.php');
36
  * @uses session_id()
37
  * @uses session_start()
38
  */
39
- function wh_init_sessions() {
40
- if (!session_id()) {
41
- session_start();
42
- }
 
 
 
 
 
43
  }
44
- add_action('init', 'wh_init_sessions');
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  /**
47
  * Hide the second step of a form. looks at hidden field 'step'.
48
  * Always show if the form is the first step.
49
  * If it's not the first step, make sure it's the next form in the steps.
50
  */
51
- function wh_hide_cf7_step_2($cf7) {
52
  $formstring = $cf7->form;
53
  //check if form has a step field
54
  if (!is_admin() && preg_match('/\[hidden step "(\d+)-(\d+)"\]/', $formstring, $matches)) {
55
- if (!isset($matches[1]) || ($matches[1] != 1 && !isset($_SESSION['step'])) || ($matches[1] != 1 && ((int) $_SESSION['step']) + 1 != $matches[1])) {
 
 
 
56
  $cf7->form = apply_filters('wh_hide_cf7_step_message', "Please fill out the form on the previous page");
57
  }
58
  if (count($matches) == 3 && $matches[1] != $matches[2]) {
59
- add_filter('wpcf7_ajax_json_echo', 'wh_clear_success_message', 10, 2);
60
  }
61
  }
62
  return $cf7;
63
  }
64
- add_action('wpcf7_contact_form', 'wh_hide_cf7_step_2');
65
 
66
  /**
67
  * Handle a multi-step cf7 form.
68
  */
69
- function wh_store_data_steps(&$cf7) {
70
  if (isset($cf7->posted_data['step'])) {
 
71
  if (preg_match('/(\d+)-(\d+)/', $cf7->posted_data['step'], $matches)) {
72
  $curr_step = $matches[1];
73
  $last_step = $matches[2];
74
- }
75
- $prev_data = isset($_SESSION['cf7_posted_data'])?$_SESSION['cf7_posted_data']:array();
 
 
 
76
  //remove empty [form] tags from posted_data so $prev_data can be stored.
77
  $fes = wpcf7_scan_shortcode();
78
  foreach ( $fes as $fe ) {
@@ -82,30 +162,47 @@ function wh_store_data_steps(&$cf7) {
82
  }
83
  if ($curr_step != $last_step) {
84
  $cf7->skip_mail = true;
85
- $_SESSION['step'] = $curr_step;
86
- $_SESSION['cf7_posted_data'] = array_merge($prev_data, $cf7->posted_data);
 
87
  }
88
  else {
89
  $cf7->posted_data = array_merge($prev_data, $cf7->posted_data);
90
- unset($_SESSION['step']);
91
- unset($_SESSION['cf7_posted_data']);
92
  }
93
  }
94
  }
95
 
96
- add_action( 'wpcf7_before_send_mail', 'wh_store_data_steps', 9 );
97
 
98
  /**
99
  * Hide success message if form is redirecting to another page.
100
  */
101
- function wh_clear_success_message($items, $result) {
102
- remove_filter('wpcf7_ajax_json_echo', 'wh_clear_success_message');
103
  if ($items['mailSent'] && isset($items['onSentOk']) && count($items['onSentOk']) > 0) {
104
  $items['onSentOk'][] = "$('" . $items['into'] . "').find('div.wpcf7-response-output').css('opacity',0);";
105
  }
106
  return $items;
107
  }
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  /************************************************************************************************************
111
  * Contact Form 7 has a nice success message after submitting its forms, but on a multi-step form,
@@ -116,8 +213,8 @@ function wh_clear_success_message($items, $result) {
116
  /**
117
  * Hide form when done.
118
  */
119
- function wh_hide_multistep_form($items, $result) {
120
- remove_filter('wpcf7_ajax_json_echo', 'wh_hide_multistep_form');
121
  if ($items['mailSent'] && !isset($items['onSentOk'])) {
122
  $items['onSentOk'] = array("$('" . $items['into'] . " form').children().not('div.wpcf7-response-output').hide();");
123
  }
@@ -127,10 +224,11 @@ function wh_hide_multistep_form($items, $result) {
127
  /**
128
  * Add filter to clear form if this is a multistep form.
129
  */
130
- function wh_cf7_before_mail($cf7) {
131
- if (isset($_SESSION['step'])) {
132
- add_filter('wpcf7_ajax_json_echo', 'wh_hide_multistep_form', 10, 2);
 
133
  }
134
  }
135
- add_action( 'wpcf7_before_send_mail', 'wh_cf7_before_mail', 8 );
136
 
5
  Description: Enables the Contact Form 7 plugin to create multi-page, multi-step forms.
6
  Author: Webhead LLC.
7
  Author URI: http://webheadcoder.com
8
+ Version: 1.1
9
  */
10
  /* Copyright 2012 Webhead LLC (email: info at webheadcoder.com)
11
 
29
  require_once(plugin_dir_path(__FILE__) . 'module-hidden.php');
30
  }
31
  require_once(plugin_dir_path(__FILE__) . 'module-session.php');
32
+ require_once(plugin_dir_path(__FILE__) . 'module-back.php');
33
 
34
  /**
35
  * init_sessions()
37
  * @uses session_id()
38
  * @uses session_start()
39
  */
40
+ function cf7msm_init_sessions() {
41
+ //try to set cookie
42
+ if ( empty( $_COOKIE['cf7msm_check'] ) ) {
43
+ setcookie('cf7msm_check', 1, 0, COOKIEPATH, COOKIE_DOMAIN);
44
+
45
+ if (!session_id()) {
46
+ session_start();
47
+ }
48
+ }
49
  }
50
+ add_action('init', 'cf7msm_init_sessions');
51
 
52
+ /**
53
+ * Add scripts to be able to go back to a previous step.
54
+ */
55
+ function cf7msm_scripts() {
56
+ $step = cf7msm_get('step');
57
+ wp_enqueue_script('cf7msm',
58
+ plugins_url('/resources/cf7msm.js', __FILE__),
59
+ array('jquery') );
60
+ wp_enqueue_style('cf7msm_styles',
61
+ plugins_url('/resources/cf7msm.css', __FILE__)
62
+ );
63
+
64
+ //this makes the script useful even when cookies aren't used.
65
+ $cf7msm_posted_data = cf7msm_get('cf7msm_posted_data');
66
+ $cf7msm_posted_data['cf7msm_prev_url'] = cf7msm_get('cf7msm_prev_url');
67
+ wp_localize_script( 'cf7msm', 'cf7msm_posted_data', $cf7msm_posted_data);
68
+ }
69
+ add_action('wp_enqueue_scripts', 'cf7msm_scripts');
70
+
71
+ /**
72
+ * Saves a variable to cookies or if not enabled, to session.
73
+ */
74
+ function cf7msm_set($var_name, $var_value) {
75
+ if ( empty( $_COOKIE['cf7msm_check'] ) ) {
76
+ $_SESSION[$var_name] = $var_value;
77
+ }
78
+ else {
79
+ setcookie($var_name, json_encode( $var_value ), 0, COOKIEPATH, COOKIE_DOMAIN);
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Get a variable from cookies or if not enabled, from session.
85
+ */
86
+ function cf7msm_get($var_name, $default = '') {
87
+ $ret = $default;
88
+ if ( empty( $_COOKIE['cf7msm_check'] ) ) {
89
+ $ret = isset( $_SESSION[$var_name] ) ? $_SESSION[$var_name] : $default;
90
+ }
91
+ else {
92
+ $ret = isset( $_COOKIE[$var_name] ) ? $_COOKIE[$var_name] : $default;
93
+ if (get_magic_quotes_gpc()) {
94
+ $ret = stripslashes($ret);
95
+ }
96
+ $ret = json_decode( $ret, true );
97
+ }
98
+ return $ret;
99
+ }
100
+
101
+ /**
102
+ * Remove a saved variable.
103
+ */
104
+ function cf7msm_remove($var_name) {
105
+ $ret = '';
106
+ if ( empty( $_COOKIE['cf7msm_check'] ) ) {
107
+ if ( isset( $_SESSION[$var_name] ) ) {
108
+ unset( $_SESSION[$var_name] );
109
+ }
110
+ }
111
+ else {
112
+ if ( isset( $_COOKIE[$var_name] ) ) {
113
+ setcookie($var_name, '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN);
114
+ }
115
+ }
116
+ }
117
+
118
+
119
  /**
120
  * Hide the second step of a form. looks at hidden field 'step'.
121
  * Always show if the form is the first step.
122
  * If it's not the first step, make sure it's the next form in the steps.
123
  */
124
+ function cf7msm_step_2($cf7) {
125
  $formstring = $cf7->form;
126
  //check if form has a step field
127
  if (!is_admin() && preg_match('/\[hidden step "(\d+)-(\d+)"\]/', $formstring, $matches)) {
128
+ $step = cf7msm_get('step');
129
+ if ( !isset($matches[1])
130
+ || ($matches[1] != 1 && empty($step) )
131
+ || ($matches[1] != 1 && ((int) $step) + 1 != $matches[1]) ) {
132
  $cf7->form = apply_filters('wh_hide_cf7_step_message', "Please fill out the form on the previous page");
133
  }
134
  if (count($matches) == 3 && $matches[1] != $matches[2]) {
135
+ add_filter('wpcf7_ajax_json_echo', 'cf7msm_clear_success_message', 10, 2);
136
  }
137
  }
138
  return $cf7;
139
  }
140
+ add_action('wpcf7_contact_form', 'cf7msm_step_2');
141
 
142
  /**
143
  * Handle a multi-step cf7 form.
144
  */
145
+ function cf7msm_store_data_steps(&$cf7) {
146
  if (isset($cf7->posted_data['step'])) {
147
+ cf7msm_set('cf7msm_prev_url', cf7msm_current_url());
148
  if (preg_match('/(\d+)-(\d+)/', $cf7->posted_data['step'], $matches)) {
149
  $curr_step = $matches[1];
150
  $last_step = $matches[2];
151
+ }
152
+ $prev_data = cf7msm_get('cf7msm_posted_data', '' );
153
+ if (!is_array($prev_data)) {
154
+ $prev_data = array();
155
+ }
156
  //remove empty [form] tags from posted_data so $prev_data can be stored.
157
  $fes = wpcf7_scan_shortcode();
158
  foreach ( $fes as $fe ) {
162
  }
163
  if ($curr_step != $last_step) {
164
  $cf7->skip_mail = true;
165
+ cf7msm_set('step', $curr_step);
166
+ $posted_data = array_merge($prev_data, $cf7->posted_data);
167
+ cf7msm_set('cf7msm_posted_data', $posted_data );
168
  }
169
  else {
170
  $cf7->posted_data = array_merge($prev_data, $cf7->posted_data);
171
+ cf7msm_remove('step');
172
+ cf7msm_remove('cf7msm_posted_data');
173
  }
174
  }
175
  }
176
 
177
+ add_action( 'wpcf7_before_send_mail', 'cf7msm_store_data_steps', 9 );
178
 
179
  /**
180
  * Hide success message if form is redirecting to another page.
181
  */
182
+ function cf7msm_clear_success_message($items, $result) {
183
+ remove_filter('wpcf7_ajax_json_echo', 'cf7msm_clear_success_message');
184
  if ($items['mailSent'] && isset($items['onSentOk']) && count($items['onSentOk']) > 0) {
185
  $items['onSentOk'][] = "$('" . $items['into'] . "').find('div.wpcf7-response-output').css('opacity',0);";
186
  }
187
  return $items;
188
  }
189
 
190
+ /**
191
+ * return the full url.
192
+ */
193
+ function cf7msm_current_url() {
194
+ $page_url = 'http';
195
+ if ($_SERVER["HTTPS"] == "on") {
196
+ $page_url .= "s";
197
+ }
198
+ $page_url .= "://";
199
+ if ($_SERVER["SERVER_PORT"] != "80") {
200
+ $page_url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
201
+ } else {
202
+ $page_url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
203
+ }
204
+ return $page_url;
205
+ }
206
 
207
  /************************************************************************************************************
208
  * Contact Form 7 has a nice success message after submitting its forms, but on a multi-step form,
213
  /**
214
  * Hide form when done.
215
  */
216
+ function cf7msm_hide_multistep_form($items, $result) {
217
+ remove_filter('wpcf7_ajax_json_echo', 'cf7msm_hide_multistep_form');
218
  if ($items['mailSent'] && !isset($items['onSentOk'])) {
219
  $items['onSentOk'] = array("$('" . $items['into'] . " form').children().not('div.wpcf7-response-output').hide();");
220
  }
224
  /**
225
  * Add filter to clear form if this is a multistep form.
226
  */
227
+ function cf7msm_cf7_before_mail($cf7) {
228
+ $step = cf7msm_get('step');
229
+ if ( !empty( $step ) ) {
230
+ add_filter('wpcf7_ajax_json_echo', 'cf7msm_hide_multistep_form', 10, 2);
231
  }
232
  }
233
+ add_action( 'wpcf7_before_send_mail', 'cf7msm_cf7_before_mail', 8 );
234
 
module-back.php ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ ** A base module for [back]
4
+ **/
5
+
6
+ /* Shortcode handler */
7
+
8
+ add_action( 'init', 'cf7msm_add_shortcode_back', 5 );
9
+
10
+ function cf7msm_add_shortcode_back() {
11
+ wpcf7_add_shortcode( 'back', 'cf7msm_back_shortcode_handler' );
12
+ }
13
+
14
+ function cf7msm_back_shortcode_handler( $tag ) {
15
+ $tag = new WPCF7_Shortcode( $tag );
16
+
17
+ $class = wpcf7_form_controls_class( $tag->type );
18
+
19
+ $atts = array();
20
+
21
+ $atts['class'] = $tag->get_class_option( $class );
22
+ $atts['id'] = $tag->get_option( 'id', 'id', true );
23
+ $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );
24
+
25
+ $value = isset( $tag->values[0] ) ? $tag->values[0] : '';
26
+
27
+ if ( empty( $value ) )
28
+ $value = __( 'Back', 'cpf7msm' );
29
+
30
+ $atts['type'] = 'button';
31
+ $atts['value'] = $value;
32
+
33
+ $atts = wpcf7_format_atts( $atts );
34
+
35
+ $html = sprintf( '<input %1$s />', $atts );
36
+
37
+ return $html;
38
+ }
39
+
40
+
41
+ /* Tag generator */
42
+
43
+ add_action( 'admin_init', 'cf7msm_add_tag_generator_back', 55 );
44
+
45
+ function cf7msm_add_tag_generator_back() {
46
+ if ( ! function_exists( 'wpcf7_add_tag_generator' ) )
47
+ return;
48
+
49
+ wpcf7_add_tag_generator( 'back', __( 'Back button', 'wpcf7' ),
50
+ 'wpcf7-cf7msm-back', 'wpcf7_cf7msm_back', array( 'nameless' => 1 ) );
51
+ }
52
+
53
+ function wpcf7_cf7msm_back( &$contact_form ) {
54
+ ?>
55
+ <div id="wpcf7-cf7msm-back" class="hidden">
56
+ <form action="">
57
+ <table>
58
+ <tr>
59
+ <td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
60
+ <input type="text" name="id" class="idvalue oneline option" /></td>
61
+
62
+ <td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
63
+ <input type="text" name="class" class="classvalue oneline option" /></td>
64
+ </tr>
65
+
66
+ <tr>
67
+ <td><?php echo esc_html( __( 'Label', 'wpcf7' ) ); ?> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
68
+ <input type="text" name="values" class="oneline" /></td>
69
+
70
+ <td></td>
71
+ </tr>
72
+ </table>
73
+
74
+ <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'wpcf7' ) ); ?><br /><input type="text" name="back" class="tag" readonly="readonly" onfocus="this.select()" /></div>
75
+ </form>
76
+ </div>
77
+ <?php
78
+ }
79
+
80
+ ?>
module-session.php CHANGED
@@ -74,8 +74,13 @@ function wpcf7_form_shortcode_handler( $tag ) {
74
  $id_att = trim( $id_att );
75
  }
76
 
 
77
  //return raw value, let filters sanitize if needed.
78
- $value = isset($_SESSION['cf7_posted_data'][$name])?$_SESSION['cf7_posted_data'][$name]:'';
 
 
 
 
79
  if (is_array($value)) {
80
  $value = implode(", ", $value);
81
  }
74
  $id_att = trim( $id_att );
75
  }
76
 
77
+ $value = '';
78
  //return raw value, let filters sanitize if needed.
79
+ $cf7msm_posted_data = cf7msm_get('cf7msm_posted_data');
80
+
81
+ if ( !empty( $cf7msm_posted_data ) && is_array( $cf7msm_posted_data ) ) {
82
+ $value = isset( $cf7msm_posted_data[$name] ) ? $cf7msm_posted_data[$name] : '';
83
+ }
84
  if (is_array($value)) {
85
  $value = implode(", ", $value);
86
  }
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: webheadllc
3
  Tags: contact form 7, multistep form, form, multiple pages, store form
4
  Requires at least: 3.4.1
5
- Tested up to: 3.5.1
6
- Stable tag: 1.02
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -33,9 +33,13 @@ the user to another page.
33
 
34
  In a contact form, to retrieve fields from previous forms you can use something like [form your-email] where "your-email" is the name of the field from the previous form. This would be useful on the last step where it is confirming all the info from previous forms.
35
 
 
 
36
  **Additional Info**
37
  The hidden field is taken directly from the "Contact Form 7 Modules". If you have that installed, the Multi-Step plugin will use that.
38
 
 
 
39
  == Frequently Asked Questions ==
40
 
41
  = Why did you do this? =
@@ -45,6 +49,11 @@ I have used countless free plugins and have saved countless hours. I could not
45
 
46
  == Changelog ==
47
 
 
 
 
 
 
48
  = 1.02 =
49
  updated version numbers.
50
 
2
  Contributors: webheadllc
3
  Tags: contact form 7, multistep form, form, multiple pages, store form
4
  Requires at least: 3.4.1
5
+ Tested up to: 3.6
6
+ Stable tag: 1.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
33
 
34
  In a contact form, to retrieve fields from previous forms you can use something like [form your-email] where "your-email" is the name of the field from the previous form. This would be useful on the last step where it is confirming all the info from previous forms.
35
 
36
+ In a contact form you users may want to go back to a previous step to change some info they entered. To allow the user to go back, add the [back "Previous Step"] button to the form.
37
+
38
  **Additional Info**
39
  The hidden field is taken directly from the "Contact Form 7 Modules". If you have that installed, the Multi-Step plugin will use that.
40
 
41
+ This plugin does not support File Uploads. If you need to use file uploads make sure to place it on the last step.
42
+
43
  == Frequently Asked Questions ==
44
 
45
  = Why did you do this? =
49
 
50
  == Changelog ==
51
 
52
+ = 1.1 =
53
+ renamed all function names to be more consistent.
54
+ use cookies before falling back to session.
55
+ added back shortcode so users can go back to previous step.
56
+
57
  = 1.02 =
58
  updated version numbers.
59
 
resources/cf7msm.css ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ .wpcf7-back {
2
+ float:left;
3
+ margin-top:5px;
4
+ position: relative;
5
+ width: 70px;
6
+ }
resources/cf7msm.js ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery(document).ready(function($) {
2
+ if (cf7msm_posted_data) {
3
+ var step_field = $("input[name='step']");
4
+ //multi step forms
5
+ if (step_field.length > 0) {
6
+ var cf7_form = $(step_field[0].form);
7
+ $.each(cf7msm_posted_data, function(key, val){
8
+ if (key == 'cf7msm_prev_url') {
9
+ cf7_form.find('.wpcf7-back').click(function(e){
10
+ window.location.href = val;
11
+ e.preventDefault();
12
+ });
13
+ }
14
+ if (key.indexOf('_') != 0 && key != 'step') {
15
+ var field = cf7_form.find('*[name="' + key + '"]');
16
+ if (field.length > 0) {
17
+ field.val(val);
18
+ }
19
+ else {
20
+ //checkbox
21
+ field = cf7_form.find('input[name="' + key + '[]"]'); //value is this or this or tihs
22
+ if (field.length > 0) {
23
+ $.each(val, function(i, v){
24
+ field.filter('input[value="' + v + '"]').prop('checked', true);
25
+ });
26
+ }
27
+ }
28
+ }
29
+ });
30
+ } //end multi step forms
31
+ }
32
+ });