wordpress contact form 7 unique id

i am currently using contact form 7 for wordpress. i would like a unique id for every form filled in and sent.

i have had a look around the internet but cant find anything, although i did find the following:

Read More

http://contactform7.com/special-mail-tags/

would there be any easy way to make my own function to do something similar to the above tags? i would need it to be a function to go into my themes function file, so that plugin updates wont affect it.

Cheers Dan

Related posts

Leave a Reply

2 comments

  1. To generate ID for the Contactform7 you need to hook the ‘wpcf7_posted_data’.

    However, to generate incremental ID you need to save the forms in database so you can retrieve which ID should be next on next Form submit. For this you will need CFDB plugin (https://cfdbplugin.com/).

    If you dont want to put the code inside theme functions.php file, you can use this plugin instead: https://wordpress.org/plugins/add-actions-and-filters/

    Example code:

    function pk_generate_ID($formName, $fieldName) {
        //Retrieve highest ID from all records for given form stored by CFDB, increment by 1 and return. 
        require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
        $start = '001';
        $exp = new CFDBFormIterator();
        $atts = array();
        $atts['show'] = $fieldName;
        $atts['filter'] = "$fieldName>=$start&&$fieldName<999999999999"; //is_numeric() is not permitted by default for CFDB filter
        $atts['limit'] = '1';
        $atts['orderby'] = "$fieldName DESC";
        $atts['unbuffered'] = 'true';
        $exp->export($formName, $atts);
        $found = $start;
        while ($row = $exp->nextRow()) {
            $row2 = $row[$fieldName];
            $row2 += 1;
            $found = max($found,$row2);
        }
        return $found;
    }
    
    
    function pk_modify_data( $posted_data){
        $formName = 'Form1'; // change this to your form's name
        $fieldName = 'ID-1'; // change this to your field ID name
    
        //Get title of the form from private property.
        $cf_sub = (array) WPCF7_Submission::get_instance();
        $cf = (array) $cf_sub["WPCF7_Submissioncontact_form"];
        $title = (string) $cf["WPCF7_ContactFormtitle"];
    
        if ( $posted_data && $title == $formName) ) { //formName match
            $posted_data[$fieldName] = pk_generate_ID($formName, $fieldName);
        }
        return  $posted_data;
    }
    
    add_filter('wpcf7_posted_data', 'pk_modify_data');