Adding custom values to Contact Form 7

Written by James McDonald

May 19, 2020

From the above URL is this code. Just putting it here so I can find it again

add_action('wpcf7_init', 'custom_code_generator');
function custom_code_generator(){
    wpcf7_add_form_tag('coupon_code', 'custom_code_handler');
function custom_code_handler($tag){
    $input_code_name = 'your_input';
    $charsList = '1234567890abcdefghijklmnopqrstuvwxyz';
    $randomString = '';
    //change the value of i to meet your requirements
    for ($i=0; $i < 10; i++){
        $randomString .= $charsList[rand(0, strlen($charsList))];
    $finalCode = strtoupper($randomString);
    //create html and return
    $html = '<input type="hidden" name="'.$input_code_name.' "value=" '.$finalCode . ' " />';
    return $html;


Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…

Scam call from “Telstra”

Just had a call from a man with a sub-continental accent from caller ID 0734946596 Told me there was problem with my...

PC Build

I recently built myself a PC using the SilverStone SG13 Case. I am planning to use it as a Hot Spare. So any client...