wordpress联系表单7唯一ID

时间:2012-03-07 10:43:10

标签: wordpress

我目前正在使用联系表单7进行wordpress。我希望每个填写并发送的表单都有一个唯一的ID。

我已经浏览过互联网,但无法找到任何东西,虽然我确实找到了以下内容:

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

是否有任何简单的方法可以让我自己的函数执行与上述标记类似的操作?我需要它作为一个功能进入我的主题功能文件,以便插件更新不会影响它。

干杯丹

2 个答案:

答案 0 :(得分:0)

要为Contactform7生成ID,您需要挂钩'wpcf7_posted_data'。

但是,要生成增量ID,您需要将表单保存在数据库中,以便在下一个表单提交时检索下一个ID。为此,您需要CFDB插件(https://cfdbplugin.com/)。

如果您不想将代码放在theme.php文件中,可以使用此插件:https://wordpress.org/plugins/add-actions-and-filters/

示例代码:

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["\0WPCF7_Submission\0contact_form"];
    $title = (string) $cf["\0WPCF7_ContactForm\0title"];

    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');

答案 1 :(得分:0)

[_ serial_number]字段是自动递增的表单提交ID。你只需要安装Flamingo插件。见http://contactform7.com/special-mail-tags/

(这个问题与问题中的链接相同,我想在问到问题时该字段不存在)。

相关问题