1

My lack of experience with both Wordpress and Javascript is tripping me up.

I have a Wordpress page that has a Jotform embedded using this tag:

<script type="text/javascript" src="https://form.jotform.com/jsform/FORMCODE"></script>

But I have a value coming into my Wordpress through the URL that I need to pass along in the embedded Jotform's URL. I cannot figure out how to do that. Do I want to use PHP? Javascript?

I tried this, which didn't work at all:

<script type="text/javascript"> 
function getURL(){
    let paramString = urlString.split('?')[1];
    let params_arr = paramString.split('&');
    let pair = params_arr[0].split('=');
    return "https://form.jotform.com/jsform/230391120310032?coursecode=" + pair[1];
}
</script>
<script type="text/javascript" src="getURL();"></script>

Thanks for any help!

1 Answers1

0

To add a javascript code to a WordPress page you need the page id else the js code will be on all the pages.

You can either add the js code to the head of the page likes so:

add_action('wp_head',function () {
  // 10 is the page id; 
  if (is_page ('10')) { 
    ?>
  <script type="text/javascript">
    function getURL(){
    let paramString = urlString.split('?')[1];
    let params_arr = paramString.split('&');
    let pair = params_arr[0].split('=');
    return "https://form.jotform.com/jsform/230391120310032?coursecode=" + pair[1];
}
 </script>
  <script type="text/javascript" src="getURL();"></script>
    <?php
  }
}
 );

Then put the js code on the footer like so:

add_action('wp_footer',function () {
  // 10 is the page id; 
  if (is_page ('10')) { 
    ?>
  <script type="text/javascript">
    function getURL(){
    let paramString = urlString.split('?')[1];
    let params_arr = paramString.split('&');
    let pair = params_arr[0].split('=');
    return "https://form.jotform.com/jsform/230391120310032?coursecode=" + pair[1];
}
 </script>
  <script type="text/javascript" src="getURL();"></script>
    <?php
  }
}
 );

Whichever snippet you pick should be added to your theme functions.php or code snippet plugin if you have one

Wisdom Ighofose
  • 357
  • 3
  • 6
  • Thanks. I just needed to get an understanding of what snippets actually were. Ended up using $_GET to grab the param and output a script tag in a code snippet (in my wordpress text element, not in the header). Appreciate the reponse. – NickMcCollumSchool Feb 11 '23 at 08:05
  • You are welcome, you can as well post your solution as the answer if mine did not really meet your need, so that others that have same issue can learn from it. – Wisdom Ighofose Feb 11 '23 at 20:37