Contact form 7 is most popular WordPress plugin to create from. Its very simple. Recently i have a request from a client he wants generate a random number in contact from subject. He want each mail generated from contact from have a ID numer, a kinda suport ticket number. So a client send a message he will simple replay without altering the subject text. Using this id he can track the message in internal CRM system.
Contact Form 7 dynamic number generation code
I have solved ths by adding a new function in to our theme function.ph file with following code.And then you can retvive the id using the field [rand-generator]
You can use this tag in mail body to how the random number. You can also use this in subject line of mail.
function wpcf7sg_generate_number( $wpcf7_data ) {
$properties = $wpcf7_data->get_properties();
$shortcode = '[sequence-generator]';
$mail = $properties['mail']['body'];
$mail_2 = $properties['mail_2']['body'];
if( preg_match( "/{$shortcode}/", $mail ) || preg_match( "/[{$shortcode}]/", $mail_2 ) ) {
$option = 'wpcf7sg_' . $wpcf7_data->id();
$sequence_number = (int)get_option( $option ) + 1;
update_option( $option, $sequence_number );
$properties['mail']['body'] = str_replace( $shortcode, $sequence_number, $mail );
$properties['mail_2']['body'] = str_replace( $shortcode, $sequence_number, $mail_2 );
$wpcf7_data->set_properties( $properties );
}
}
add_action( 'wpcf7_before_send_mail', 'wpcf7sg_generate_number' );