重力形式创建唯一 ID

Gravity Forms create Unique ID

在我的 wordpress 网站上,我安装了 Gravity Forms。为了让表单创建一个 ID,我将这段代码放在子主题中:

add_filter("gform_field_value_uuid", "get_unique");

function get_unique(){

$prefix = "VFI"; // update the prefix here

do {
    $unique = mt_rand();
    $unique = substr($unique, 0, 8);
    $unique = $prefix . $unique;
} while (!check_unique($unique));

return $unique;
}

function check_unique($unique) {
global $wpdb;

$table = $wpdb->prefix . 'rg_lead_detail';
$form_id = 1; // update to the form ID your unique id field belongs to
$field_id = 93; // update to the field ID your unique id is being prepopulated in
$result = $wpdb->get_var("SELECT value FROM $table WHERE form_id = '$form_id' AND field_number = '$field_id' AND value = '$unique'");

if(empty($result))
    return true;

return false;
}

此代码有效,并没有给我带来太多麻烦。由于我将重力形式的所有信息传递给 mailchimp,我注意到在我的 Mailchiimp 列表中一些唯一 ID(例如:VFI819231)被重新放置。

在质疑其功能之前,这段代码中是否有任何可能导致此问题的内容?因为我正在做一些测试,所以创建一个用户然后删除它,然后再次添加它所以我想知道是不是因为这个。

感谢您的帮助!

此代码应该有效。 GP Unique ID,我编写的用于处理生成各种唯一 ID 的 Gravity Forms 插件,以类似的方式处理此问题。要注意的一件事是删除条目。由于 check_unique() 函数正在 ping 条目数据库以确保生成的 ID 是唯一的,因此删除条目将允许生成重复的 ID。

  1. 将此代码添加到您的 functions.php 文件中。

  2. 创建您的表单并添加一个隐藏字段并允许动态填充它(高级选项卡)。

  3. 在参数名称文本字段中键入“uuid”。

    add_filter("gform_field_value_uuid", "uuid");      
    function uuid($prefix = '') {
        $chars = md5(uniqid(mt_rand(), true));
        $uuid  = substr($chars,0,8) . '-';
        $uuid .= substr($chars,8,4) . '-';
        $uuid .= substr($chars,12,4) . '-';
        $uuid .= substr($chars,16,4) . '-';
        $uuid .= substr($chars,20,12);
        return $prefix . $uuid;
    }