Form Target URL Driven by Wordpress Custom Field

Hi!
We’re looking for a way for our client to add their preferred target URL into a custom field on a post, which then defines the redirect of the form (using Hubspot embed as the client prefers this to using something like Gravity etc).

Any help appreciated!

Hi, @SClick :waving_hand: Thanks for the question. Hey, @Anton @louischausse, have you tacked anything similar? Thank you for taking a look! — Jaycee

We’ve actually got this working!
<script charset=“utf-8” type=“text/javascript” src=“//js.hsforms.net/forms/v2-legacy.js”></script>
<![endif]-->
<script charset=“utf-8” type=“text/javascript” src=“//js.hsforms.net/forms/v2.js”></script>
<script>
hbspt.forms.create({
portalId: “xxxxxxx”,
formId: “xxxxxxxx-xxxx-xxxxxxxx-xxxxxxxxx”,
inlineMessage: ‘Success’,
onFormSubmit: function($form){
setTimeout( function() {
var formData = $form.serialize();
window.location = “[acf field=“target_url”]”
}, 250 ); // Redirects to url from advancved custom fields after 250 milliseconds.
}
});
</script>

Awesome!

just one recommendation: Change “onFormSubmit” to “onFormSubmitted” since this will trigger the redirect after the form is submitted. When you’re working with “onFormSubmit” submissions can get lost/not be submitted and the user/visitor is just redirected

best,

Anton

Ah yes amazing thank you for your help!

Hi @SClick,

you can modify the embed code with the “onFormSubmitted” option from the advanced form options.

default embed code:

<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
 hbspt.forms.create({
 region: "na1",
 portalId: "xxxxxxx",
 formId: "xxxxxxxxxx",
 version: "V2_PRERELEASE"
 });
</script>

modified embed code:

<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
 hbspt.forms.create({
 region: "na1",
 portalId: "xxxxxxx",
 formId: "xxxxxxxxxx",
 version: "V2_PRERELEASE",
 onFormSubmitted: "https://yourredirect.com"
 });
</script>

hope this helps

best,

Anton