将 URL 参数拉入 WordPress "Raw HTML" 内容元素

Pull URL parameters into a WordPress "Raw HTML" content element

我正在使用 URL Params 插件通过短代码将参数提取到常规内容中。但是我必须使用 Raw HTML 块将 Typeform 代码插入到页面中,并且我希望能够将 URL 参数传递到 Typeform 代码中以跟踪表单提交的来源。

我不知道该怎么做。该表格在以下位置运行良好:https://HelloExit.com/instant-valuation

但我希望能够派人到 https://HelloExit.com/instant-valuation/?source=XXXX 并将 XXXX 作为 "data-url"

中的 "source" 值拉入 Typeform 代码

这是我尝试过的:

<script>
  function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
      vars[key] = value;
    });
    return vars;
  }
  var source = getUrlVars()["source"];
</script>
    
<div
  class="typeform-widget"
  data-url="https://xgenius.typeform.com/to/zZHPPk?source=<script>document.write(source)</script>"
  data-transparency="100"
  data-hide-headers=true
  data-hide-footer=true
  style="width: 100%; height: 500px;">
</div>
    
<!-- Typeform embed code -->
<script>(function() { var qs,js,q,s,d=document, gi=d.getElementById, 
ce=d.createElement, gt=d.getElementsByTagName, id="typef_orm", 
b="https://embed.typeform.com/"; if(!gi.call(d,id)) { js=ce.call(d,"script"); js.id=id; 
js.src=b+"embed.js"; q=gt.call(d,"script")[0]; q.parentNode.insertBefore(js,q) } })() 
</script><div style="font-family: Sans-Serif;font-size: 12px;color: #999;opacity: 0.5;padding-top: 5px;"> powered by <a href="https://admin.typeform.com/signup?utm_campaign=zZHPPk&utm_source=typeform.com-01D8JVB4T91A26RA6WEZRZVF05- pro&utm_medium=typeform&utm_content=typeform-embedded-poweredbytypeform&utm_term=EN" style="color: #999" target="_blank">Typeform</a></div>

如有任何帮助,我们将不胜感激!

你很接近,但你需要使用 Javascript 来改变你的 div 的 data-url 属性。

// ...
var source = getUrlVars()["source"];

// concatenate the url with your source variable
var newUrl = `https://xgenius.typeform.com/to/zZHPPk?source=${source}`;

// get the element whose attributes you want to dynamically set
// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
var widgetElement = document.querySelector('.typeform-widget');

// set the source attribute
// https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
widgetElement.setAttribute('data-url', newUrl);

仔细测试它,因为它可能仍然以竞争条件结束(也就是说,Typeform 嵌入代码可能会在您更新它引用的 data-url 属性之前开始 运行) .