Here is a very easy way of doing this:
Just follow the instructions at the top of the script.
<?php
/**
** A base module for [hidden customfield]
**
**--Instructions--
** Put this script as a file (hidden_custom.php) into the plugins/contact-form-7/modules/ folder.
** "Hidden Custom Field" should now be an option in the "Generate Tag" dropdown of your contact form.
** Type the name of your custom field in the "The Custom Field name goes here" field.
** Put the 2 generated into the form and email as indicated.
**
**/
// Shortcode handler
wpcf7_add_shortcode( hidden, 'wpcf7_hidden_shortcode_handler', true );
function wpcf7_hidden_shortcode_handler( $tag ) {
if ( ! is_array( $tag ) )
return '';
$name = $tag['name'];
$options = (array) $tag['options'];
$values = (array) $tag['values'];
$atts = '';
$id_att = '';
$class_att .= ' wpcf7-hidden';
foreach ( $options as $option ) {
if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
$id_att = $matches[1];
}
}
if ( $id_att )
$atts .= ' id="' . trim( $id_att ) . '"';
global $wp_query;
$postid = $wp_query->post->ID;
$theCustomValue = get_post_meta($postid, $name , true);
$value = $theCustomValue;
$html = '<input name="'.$name.'" type="hidden" value="' . esc_attr( $value ) . '"' . $atts . ' />';
return $html;
}
// Tag generator
add_action( 'admin_init', 'wpcf7_add_tag_generator_hidden', 50 );
function wpcf7_add_tag_generator_hidden() {
wpcf7_add_tag_generator( 'hidden', __( 'Hidden Custom Field', 'wpcf7' ),
'wpcf7-tg-pane-hidden', 'wpcf7_tg_pane_hidden' );
}
function wpcf7_tg_pane_hidden( &$contact_form ) {
?>
<div id="wpcf7-tg-pane-hidden" class="hidden">
<form action="">
<table>
<tr><td><?php echo esc_html( __( 'The Custom Field name goes here', 'wpcf7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr>
</table>
<!-- I am killing these because they are not needed. I am leaving them in case someone wants to modify this code.
<table>
<tr>
<td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
<input type="text" name="id" class="idvalue oneline option" /></td>
<td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
<input type="text" name="class" class="classvalue oneline option" /></td>
</tr>
</table>
-->
<div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'wpcf7' ) ); ?><br /><input type="text" name="hidden" class="tag" readonly="readonly" onfocus="this.select()" /></div>
<div class="tg-mail-tag"><?php echo esc_html( __( "And, put this code anywhere in the emails below.", 'wpcf7' ) ); ?><br /><span class="arrow" style="font-size:20px;">→</span> <input type="text" class="mail-tag" readonly="readonly" onfocus="this.select()" /></div>
</form>
</div>
<?php
}
?>