• Resolved Jeff Stevens

    (@jsrestaurantlink)


    When I use [insert_php] and populate a variable, is it available outside of the [insert_php] block? Like in my <script>

    ie.

    <head></head>
    <body>
      [insert_php]
        $testVar = "Hello";
        echo $testVar . "\n";
      [/insert_php]
      <script type="text/javascript">
        document.write($testVar);
      </script>
    </body>

    only produces
    Hello

    It should produce
    Hello
    Hello

    Correct?

    Am I missing something?

    Thanks!

    https://wordpress.org/plugins/insert-php/

Viewing 4 replies - 1 through 4 (of 4 total)
  • WillBontrager

    (@willbontrager)

    Sorry, no variable created in an [insert_php] block is available outside that block. More information here:

    http://www.willmaster.com/software/WPplugins/insert-php-wordpress-plugin-instructions.php#functionscope

    To use it in a script tag, it can be done something like this:

    [insert_php]
    $testVar = "Hello";
    echo $testVar . "\n";
    echo '<script type="text/javascript">';
    echo "document.write($testVar);";
    echo '</script>';
     [/insert_php]

    Will

    Thread Starter Jeff Stevens

    (@jsrestaurantlink)

    Thanks for the reply – that’s kind of what I thought also – and had tried something like that before. Here is some more code and I still just get one “Hello” – and we should get two – right?

    ...
           $testVar = "Hello";
           echo $testVar . "\n";
           echo '<script type="text/javascript">';
           echo "document.write($testVar);";
           echo '</script>';
           echo "PHP Code out...";
        [/insert_php]
    WillBontrager

    (@willbontrager)

    Ah the PHP variable needs to be quoted so JavaScript treats it as a string.

    echo "document.write('$testVar');";

    Your JavaScript console should have told you about the error.

    Will

    Thread Starter Jeff Stevens

    (@jsrestaurantlink)

    Ah – you’re correct. Thanks so much.
    I’ll check my console more often also.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Variables available in after [insert_php]’ is closed to new replies.