You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress Avada主题下无核心修改实现Salesforce表单URL字段赋值

Absolutely! Using window.onload (or even a more efficient modern alternative) is a perfect solution here—no need to modify core theme files or struggle with body tag attributes. Let me break this down step by step so you can implement it easily.

Step 1: Add the Hidden URL Field to Your Form

First, insert the required hidden field into your existing form. Place it alongside your other hidden fields for consistency (I’ve added it right after the retURL field in the example below):

<form action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">
  <input type=hidden name="oid" value="00DU0000000H99p">
  <input type=hidden name="retURL" value="https://www.sablesys.com/thank-you/">
  <!-- New hidden URL field -->
  <input type="hidden" id="URL" name="URL" value="" >
  <input type="hidden" name="member_status" value="Sent">
  <!-- Campaign id for Quote Campaign -->
  <input type="hidden" name="Campaign_ID" value="7010B000001Eiz3QAC" />
  <input type="hidden" id="lead_source" name="lead_source" value="Website">
  
  <!-- Rest of your form fields -->
  <label for="first_name">First Name</label><input id="first_name" maxlength="40" name="first_name" size="20" type="text" required><br>
  <label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" size="20" type="text" required><br>
  <label for="company">Company</label><input id="company" maxlength="40" name="company" size="20" type="text" required><br>
  <label for="email">Email</label><input id="email" maxlength="80" name="email" size="20" type="text" required><br>
  <label for="city">City</label><input id="city" maxlength="40" name="city" size="20" type="text" required><br>
  Notes:<br><textarea id="00NU0000003mN8A" name="00NU0000003mN8A" rows="25" type="text" wrap="soft" style="width: 100%"></textarea><br>
  <input type="submit" name="submit">
</form>

Step 2: Add the JavaScript to Populate the Field

Instead of relying on a body onload attribute, we’ll use a standalone script that runs automatically when the page is ready. I recommend using DOMContentLoaded (it triggers as soon as the page’s HTML is loaded, faster than window.onload which waits for images and all resources).

Here’s the code:

document.addEventListener('DOMContentLoaded', function() {
  // Find the URL field by its ID
  const urlField = document.getElementById('URL');
  
  // Only run the code if the field exists (avoids errors on pages without the form)
  if (urlField) {
    // Assign the current page's full URL to the field's value
    urlField.value = window.location.href;
  }
});

Step 3: Insert the Script into Your Target Pages (No Core File Edits!)

Since you only need this on pages with the form, use Avada’s built-in page-level custom code options to avoid affecting your entire site:

  1. Open the WordPress editor for the page containing your form.
  2. Look for the Avada Page Options panel on the right (if it’s hidden, click the top-right "Screen Options" checkbox to enable it).
  3. Expand the Custom CSS & JS section.
  4. Paste the JavaScript code into the Custom JavaScript text box.
  5. Click "Update" to save the page.

Alternatively, if you’re using the Gutenberg editor, you can add a Custom HTML block directly below your form and wrap the script in <script> tags:

<script>
document.addEventListener('DOMContentLoaded', function() {
  const urlField = document.getElementById('URL');
  if (urlField) {
    urlField.value = window.location.href;
  }
});
</script>

How This Works

  • The script waits until the page’s HTML is fully loaded before running, so it can reliably find the hidden URL field.
  • The if (urlField) check ensures the code only runs on pages where the form exists—no console errors on other pages.
  • window.location.href grabs the full URL of the current page (including any query parameters) and assigns it to the field’s value, which will be sent to Salesforce when the form is submitted.

Quick Test

After setting this up, load the page with your form, right-click and select "Inspect" to open your browser’s dev tools. Navigate to the "Elements" tab, find the hidden URL field, and confirm its value attribute matches the page’s URL. Submit the form and check Salesforce to verify the URL is being captured correctly.

内容的提问来源于stack exchange,提问作者Ash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:49:00