Skip Navigation

[Resolved] Passing a custom field to the JS in a template

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to pass a custom field value to their JS code.

Solution:

In order to do this you will need to scrape the value from the page display itself using Javascript. You are not able to add the shortcode directly to the js code.

https://stackoverflow.com/questions/24841893/get-p-tag-content-using-jquery

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Timothy 5 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#1253093

Hi,

I'm using Contact Form 7 and have embedded a form in a Content Template. The Content Template is for a CPT called Articles. The Articles have a custom field for URL. Upon submission of the form I'm trying to redirect to that url.

This is the code I'm using in the JS panel:
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = '[types field='link-to-article' output='raw'][/types]';
}, false );

And this is a sample page:
hidden link

The redirect is not working though.

Tim

#1253147

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

Thank you for getting in touch.

Actually this is not valid code.
[php]
location = '[types field='link-to-article' output='raw'][/types]';
[php]

Shortcodes can't be rendered in JS. What you need to do is to load the shortcode on the page itself and then use JS to get the value from the page.

https://stackoverflow.com/questions/5617952/jquery-how-do-i-get-the-url-of-an-anchor-contained-within-a-clicked-li-tag

Have a look at this link above.

Thanks,
Shane

#1253249

Thanks Shane, I don't really know Javascript but I managed to get the URL to appear in an alert box, so I can grab it from the page. Now I guess I need to pass it to the code I had, but this doesn't work... am I close? This

var myLink = document.getElementById("myLink");
var myURL = myLink.href; 

document.addEventListener( 'wpcf7mailsent', function( event ) {
location = (myURL);
}, false ); 

This causes a redirect but just append myURL to the existing url.

Tim

#1253253

oh, wait, actually that does work.

#1253403

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

Could you send me a link to the page where you have the link ?

Also please let me have a screenshot of the exact area where the link is so that I can use some js to get the link for you.

Thanks,
Shane

#1254353

My code above worked, thanks!