doubei3312 2016-04-11 16:49
浏览 35
已采纳

重力表单创建唯一ID

on my wordpress site I installed Gravity Forms. To have the form create an ID I placed this code in the child theme:

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;
}

This code is working and it's not giving me much trouble. Since I pass all the info from gravity forms to mailchimp, I noticed on my Mailchiimp list that some of the Unique ID (eg: VFI819231) are replcated.

Before questioning its functionality, is there anything on this code that can cause this? Cause I'm doing some test, so creating an user then deleting it, then adding it again so I wonder if it's because this.

Thanks for helping!

  • 写回答

1条回答 默认 最新

  • doutan8775 2016-04-11 20:26
    关注

    This code should work. GP Unique ID, a Gravity Forms plugin I wrote to handle generating unique IDs of all kinds, handles this in a similar way. The one thing to look out for is deleting entries. Since the check_unique() function is pinging the entries database to make sure the generated id is unique, deleting entries would allow for duplicate IDs to be generated.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置