【问题标题】:wordpress contact form 7 unique idwordpress 联系表 7 唯一 ID
【发布时间】:2012-03-07 10:43:10
【问题描述】:

我目前正在为 wordpress 使用联系表 7。我想为每个填写和发送的表格提供一个唯一的 ID。

我浏览了互联网,但找不到任何东西,尽管我确实找到了以下内容:

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

是否有任何简单的方法可以让我自己的函数执行类似于上述标签的操作?我需要它成为进入我的主题函数文件的函数,这样插件更新就不会影响它。

干杯丹

【问题讨论】:

  • 你不能使用日期和时间来制作一个 uniq ID 吗?
  • 这是我打算做的,但是客户想要一个递增的 id,所以提交的第一个表单是 id1,第二个是 id2 等等
  • 那么我的回答是否定的,这并不容易。并告诉客户需要更多时间(/money)
  • 你看过这个wordpress.org/extend/plugins/wats吗?

标签: wordpress


【解决方案1】:

[_serial_number] 字段是一个自动递增的表单提交 ID。您只需要安装 Flamingo 插件即可。见http://contactform7.com/special-mail-tags/

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

【讨论】:

    【解决方案2】:

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

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

    如果你不想将代码放在主题functions.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');
    

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 1970-01-01
      • 2020-05-30
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 2015-11-24
      相关资源
      最近更新 更多