Contact Form 7 Dynamic Text Extension - Version 1.0.1

Version Description

  • Fixed dependency issue.

=

Download this release

Release Info

Developer sevenspark
Plugin Icon wp plugin Contact Form 7 Dynamic Text Extension
Version 1.0.1
Comparing to
See all releases

Version 1.0.1

Files changed (3) hide show
  1. readme.txt +115 -0
  2. screenshot-1.jpg +0 -0
  3. wpcf7_dynamic_text.php +246 -0
readme.txt ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Contact Form 7 Dynamic Text Extension ===
2
+ Contributors: sevenspark
3
+ Donate link: http://bit.ly/bVogDN
4
+ Tags: Contact Form 7, Contact, Contact Form, dynamic, text, input, GET,
5
+ Requires at least: 2.9
6
+ Tested up to: 3.0.1
7
+ Stable tag: 1.0.1
8
+
9
+ This plugin provides a new tag type for the Contact Form 7 Plugin. It allows the dynamic generation of content for a text input box via any shortcode.
10
+
11
+ == Description ==
12
+
13
+ Contact Form 7 is an excellent WordPress plugin that provides all sorts of flexible features for creating Contact Forms. I've used it on a variety
14
+ of sites with great success.
15
+
16
+ One thing it doesn't handle, however, is dynamic content. Any default values in Contact Form 7 are static. But what if I wanted a Contact Form
17
+ with pre-populated fields based on some other value? Some examples might include:
18
+
19
+ * Auto-filling a URL
20
+ * Auto-filling a Post ID
21
+ * Pre-populating a Product Number
22
+ * Referencing other content on the site
23
+
24
+ There are many more case-specific examples. I searched for a solution, and there are some decent hacks out there. Many of them are explored in this
25
+ forum topic: [Contact Form 7 Input Fields Values as PHP Get-Viarables](http://wordpress.org/support/topic/contact-form-7-input-fields-values-as-php-get-viarables).
26
+ However, they all involved hacking the current Contact Form 7 code, which means next time the plugin is updated their edits will be overwritten. Oops.
27
+
28
+ This Dynamic Text Extension plugin provides a more elegant solution that leaves the Contact Form 7 Plugin intact.
29
+
30
+ = WHAT DOES IT DO? =
31
+
32
+ This plugin provides a new tag type for the Contact Form 7 Plugin. It allows the dynamic generation of content for a text input box via any shortcode.
33
+ For example, it comes with two built-in shortcodes that will allow the Contact Form to be populated from any $_GET PHP variable or any info from the
34
+ get_bloginfo() function.
35
+
36
+ = HOW TO USE IT =
37
+
38
+ After installing and activating the plugin, the Contact Form 7 tag generator will have a new tag type: Dynamic Text Field. Most of the options will be
39
+ familiar to Contact Form 7 users. There are two important fields:
40
+
41
+ **Dynamic Value**
42
+
43
+ This field takes a shortcode, with two important provisions:
44
+
45
+ 1. The shortcode should NOT include the normal square brackets ([ and ]). So, instead of [CF7_GET key='value'] you would use CF7_GET key='value' .
46
+ 2. Any parameters in the shortcode must use single quotes. That is: CF7_GET key='value' and not CF7_GET key="value"
47
+
48
+ **Uneditable Option**
49
+
50
+ As these types of fields should often remain uneditable by the user, there is a checkbox to turn this option on.
51
+
52
+
53
+ = INCLUDED SHORTCODES =
54
+
55
+ The plugin includes 2 basic shortcodes for use with the Dynamic Text extension. You can write your own as well - any shortcode will work
56
+
57
+ **PHP GET Variables**
58
+
59
+ Want to use a variable from the PHP GET array? Just use the CF7_GET shortcode. For example, if you want to get the foo parameter from the url
60
+ http://mysite.com?foo=bar
61
+
62
+ Enter the following into the "Dynamic Value" input
63
+
64
+ CF7_GET key='foo'
65
+ Your Content Form 7 Tag will look something like this:
66
+
67
+ [dynamictext dynamicname "CF7_GET key='foo'"]
68
+
69
+ Your form's dynamicname text input will then be pre-populated with the value of foo, in this case, bar
70
+
71
+ **Blog Info**
72
+
73
+ Want to grab some information from your blog like the URL or the sitename? Use the CF7_bloginfo shortcode. For example, to get the site's URL:
74
+
75
+ Enter the following into the "Dynamic Value" input
76
+
77
+ CF7_bloginfo show='url'
78
+
79
+ Your Content Form 7 Tag will look something like this:
80
+
81
+ [dynamictext dynamicname "CF7_bloginfo show='url'"]
82
+
83
+ Your form's dynamicname text input will then be pre-populated with your site's URL
84
+
85
+
86
+
87
+ == Installation ==
88
+
89
+ This section describes how to install the plugin and get it working.
90
+
91
+ 1. Download and install the Contact Form 7 Plugin located at http://wordpress.org/extend/plugins/contact-form-7/
92
+ 1. Upload the plugin folder to the `/wp-content/plugins/` directory
93
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
94
+ 1. You will now have a "Dynamic Text" tag option in the Contact Form 7 tag generator
95
+
96
+
97
+ == Frequently Asked Questions ==
98
+
99
+ None. Yet.
100
+
101
+
102
+ == Screenshots ==
103
+
104
+ 1. The new Dynamic Text Field options.
105
+
106
+
107
+ == Changelog ==
108
+
109
+ = 1.0.1 =
110
+ * Fixed dependency issue.
111
+
112
+
113
+ == Upgrade Notice ==
114
+
115
+ Nothing yet.
screenshot-1.jpg ADDED
Binary file
wpcf7_dynamic_text.php ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /*
4
+ Plugin Name: Contact Form 7 - Dynamic Text Extension
5
+ Plugin URI: http://http://sevenspark.com/wordpress-plugins/contact-form-7-dynamic-text-extension
6
+ Description: Provides a dynamic text field that accepts any shortcode to generate the content. Requires Contact Form 7
7
+ Version: 1.0.1
8
+ Author: Chris Mavricos, SevenSpark
9
+ Author URI: http://sevenspark.com
10
+ License: GPL2
11
+ */
12
+
13
+ /* Copyright 2010 Chris Mavricos, SevenSpark (email : chris@sevenspark.com)
14
+
15
+ This program is free software; you can redistribute it and/or modify
16
+ it under the terms of the GNU General Public License, version 2, as
17
+ published by the Free Software Foundation.
18
+
19
+ This program is distributed in the hope that it will be useful,
20
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ GNU General Public License for more details.
23
+
24
+ You should have received a copy of the GNU General Public License
25
+ along with this program; if not, write to the Free Software
26
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
+ */
28
+
29
+
30
+ /**
31
+ ** A base module for [dynamictext], [dynamictext*]
32
+ **/
33
+ function wpcf7_dynamictext_init(){
34
+ if(function_exists('wpcf7_add_shortcode')){
35
+
36
+ /* Shortcode handler */
37
+
38
+ wpcf7_add_shortcode( 'dynamictext', 'wpcf7_dynamictext_shortcode_handler', true );
39
+ wpcf7_add_shortcode( 'dynamictext*', 'wpcf7_dynamictext_shortcode_handler', true );
40
+
41
+ }
42
+ }
43
+ add_action( 'plugins_loaded', 'wpcf7_dynamictext_init');
44
+
45
+ function wpcf7_dynamictext_shortcode_handler( $tag ) {
46
+ global $wpcf7_contact_form;
47
+
48
+ if ( ! is_array( $tag ) )
49
+ return '';
50
+
51
+ $type = $tag['type'];
52
+ $name = $tag['name'];
53
+ $options = (array) $tag['options'];
54
+ $values = (array) $tag['values'];
55
+
56
+ if ( empty( $name ) )
57
+ return '';
58
+
59
+ $atts = '';
60
+ $id_att = '';
61
+ $class_att = '';
62
+ $size_att = '';
63
+ $maxlength_att = '';
64
+ $tabindex_att = '';
65
+
66
+ $class_att .= ' wpcf7-text';
67
+
68
+ if ( 'dynamictext*' == $type )
69
+ $class_att .= ' wpcf7-validates-as-required';
70
+
71
+ foreach ( $options as $option ) {
72
+ if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
73
+ $id_att = $matches[1];
74
+
75
+ } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
76
+ $class_att .= ' ' . $matches[1];
77
+
78
+ } elseif ( preg_match( '%^([0-9]*)[/x]([0-9]*)$%', $option, $matches ) ) {
79
+ $size_att = (int) $matches[1];
80
+ $maxlength_att = (int) $matches[2];
81
+
82
+ } elseif ( preg_match( '%^tabindex:(\d+)$%', $option, $matches ) ) {
83
+ $tabindex_att = (int) $matches[1];
84
+
85
+ }
86
+ }
87
+
88
+ if ( $id_att )
89
+ $atts .= ' id="' . trim( $id_att ) . '"';
90
+
91
+ if ( $class_att )
92
+ $atts .= ' class="' . trim( $class_att ) . '"';
93
+
94
+ if ( $size_att )
95
+ $atts .= ' size="' . $size_att . '"';
96
+ else
97
+ $atts .= ' size="40"'; // default size
98
+
99
+ if ( $maxlength_att )
100
+ $atts .= ' maxlength="' . $maxlength_att . '"';
101
+
102
+ if ( '' !== $tabindex_att )
103
+ $atts .= sprintf( ' tabindex="%d"', $tabindex_att );
104
+
105
+ // Value
106
+ if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) && $wpcf7_contact_form->is_posted() ) {
107
+ if ( isset( $_POST['_wpcf7_mail_sent'] ) && $_POST['_wpcf7_mail_sent']['ok'] )
108
+ $value = '';
109
+ else
110
+ $value = stripslashes_deep( $_POST[$name] );
111
+ } else {
112
+ $value = isset( $values[0] ) ? $values[0] : '';
113
+ }
114
+
115
+ $value = do_shortcode('['.$value.']');
116
+ //echo '<pre>'; print_r($options);echo '</pre>';
117
+ $readonly = '';
118
+ if(in_array('uneditable', $options)){
119
+ $readonly = 'readonly="readonly"';
120
+ }
121
+
122
+ $html = '<input type="text" name="' . $name . '" value="' . esc_attr( $value ) . '"' . $atts . ' '. $readonly.' />';
123
+
124
+ $validation_error = '';
125
+ if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
126
+ $validation_error = $wpcf7_contact_form->validation_error( $name );
127
+
128
+ $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
129
+
130
+ return $html;
131
+ }
132
+
133
+
134
+ /* Validation filter */
135
+
136
+ add_filter( 'wpcf7_validate_dynamictext', 'wpcf7_dynamictext_validation_filter', 10, 2 );
137
+ add_filter( 'wpcf7_validate_dynamictext*', 'wpcf7_dynamictext_validation_filter', 10, 2 );
138
+
139
+ function wpcf7_dynamictext_validation_filter( $result, $tag ) {
140
+ global $wpcf7_contact_form;
141
+
142
+ $type = $tag['type'];
143
+ $name = $tag['name'];
144
+
145
+ $_POST[$name] = trim( strtr( (string) $_POST[$name], "\n", " " ) );
146
+
147
+ if ( 'dynamictext*' == $type ) {
148
+ if ( '' == $_POST[$name] ) {
149
+ $result['valid'] = false;
150
+ $result['reason'][$name] = $wpcf7_contact_form->message( 'invalid_required' );
151
+ }
152
+ }
153
+
154
+ return $result;
155
+ }
156
+
157
+
158
+ /* Tag generator */
159
+
160
+ add_action( 'admin_init', 'wpcf7_add_tag_generator_dynamictext', 15 );
161
+
162
+ function wpcf7_add_tag_generator_dynamictext() {
163
+ wpcf7_add_tag_generator( 'dynamictext', __( 'Dynamic Text field', 'wpcf7' ),
164
+ 'wpcf7-tg-pane-dynamictext', 'wpcf7_tg_pane_dynamictext_' );
165
+
166
+ }
167
+
168
+ function wpcf7_tg_pane_dynamictext_( &$contact_form ) {
169
+ wpcf7_tg_pane_dynamictext( 'dynamictext' );
170
+ }
171
+
172
+ function wpcf7_tg_pane_dynamictext( $type = 'dynamictext' ) {
173
+ ?>
174
+ <div id="wpcf7-tg-pane-<?php echo $type; ?>" class="hidden">
175
+ <form action="">
176
+ <table>
177
+ <tr><td><input type="checkbox" name="required" />&nbsp;<?php echo esc_html( __( 'Required field?', 'wpcf7' ) ); ?></td></tr>
178
+ <tr><td><?php echo esc_html( __( 'Name', 'wpcf7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr>
179
+ </table>
180
+
181
+ <table>
182
+ <tr>
183
+ <td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
184
+ <input type="text" name="id" class="idvalue oneline option" /></td>
185
+
186
+ <td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
187
+ <input type="text" name="class" class="classvalue oneline option" /></td>
188
+ </tr>
189
+
190
+ <tr>
191
+ <td><code>size</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
192
+ <input type="text" name="size" class="numeric oneline option" /></td>
193
+
194
+ <td><code>maxlength</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
195
+ <input type="text" name="maxlength" class="numeric oneline option" /></td>
196
+ </tr>
197
+
198
+ <tr>
199
+ <td>
200
+ <input type="checkbox" name="uneditable" class="option" />&nbsp;<?php echo esc_html( __( "Make this field Uneditable", 'wpcf7' ) ); ?><br />
201
+ </td>
202
+
203
+ <td><?php echo esc_html( __( 'Dynamic value', 'wpcf7' ) ); ?> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br /><input type="text" name="values" class="oneline" />
204
+ <?php echo esc_html( __( 'You can enter any short code. Just leave out the square brackets ([]) and only use single quotes (\' not ")', 'wpcf7' )); ?>
205
+ </td>
206
+ </tr>
207
+ </table>
208
+
209
+ <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'wpcf7' ) ); ?><br /><input type="text" name="<?php echo $type; ?>" class="tag" readonly="readonly" onfocus="this.select()" /></div>
210
+
211
+ <div class="tg-mail-tag"><?php echo esc_html( __( "And, put this code into the Mail fields below.", 'wpcf7' ) ); ?><br /><span class="arrow">&#11015;</span>&nbsp;<input type="text" class="mail-tag" readonly="readonly" onfocus="this.select()" /></div>
212
+ </form>
213
+ </div>
214
+ <?php
215
+ }
216
+
217
+
218
+
219
+ /*
220
+ * Used like this:
221
+ *
222
+ * CF7_GET val='value'
223
+ *
224
+ * No [] and single quotes ' rather than double "
225
+ *
226
+ */
227
+ function cf7_get($atts){
228
+ extract(shortcode_atts(array(
229
+ 'key' => 0,
230
+ ), $atts));
231
+ $auction = urldecode($_GET[$key]);
232
+ return $auction;
233
+ }
234
+ add_shortcode('CF7_GET', 'cf7_get');
235
+
236
+ /* See http://codex.wordpress.org/Function_Reference/get_bloginfo */
237
+ function cf7_bloginfo($atts){
238
+ extract(shortcode_atts(array(
239
+ 'show' => 'name'
240
+ ), $atts));
241
+
242
+ return get_bloginfo($show);
243
+ }
244
+ add_shortcode('CF7_bloginfo', 'cf7_bloginfo');
245
+
246
+ ?>