• I am stumped on how to accomplish something within the WP framework… I am running 4.6.1 with the Divi theme (if that makes a difference).

    What I need to do: capture a URL parameter, then pass that parameter in a “<script>” tag. I have tried using a URL parameter plugin which will capture the parameter but seems to have trouble within the script tag. I have also tried a couple of PHP code plugins but they don’t seem to work either.

    Pre-WordPress, I have been doing this by using the PHP GET function in the “<head>” section of my PHP page, then storing to a variable, then echoing the variable within the script tag.

    I just need a workable solution to do this from within a WP page. Thanks for any input.

Viewing 1 replies (of 1 total)
  • How about something like this in your page template:

    If the URL query string is ?my_varname=myvalue

    <?php
    $my_variable = htmlspecialchars($_GET["my_varname"]);
    ?>
    
    <script type="text/javascript">
    var my_js_variable = "<?php echo esc_js($my_variable); ?>";
    </script>
Viewing 1 replies (of 1 total)

The topic ‘Passing variable from URL to script tag’ is closed to new replies.