Support » Developing with WordPress » how to pass value from php variable inside temlate to JS footer script variable

  • Resolved snippet24

    (@snippet24)


    I’m getting this error from console log on modoShortcode var

    <br />
    <b>Notice</b>:  Undefined variable: modoshortcode1 in <b>E:**footer.php</b> on line <b>164</b><br />

    when using
    <?php $modoshortcode1 = "lista"; $paginashortcode1 = "2";
    inside index.php

    and in footer.php

    <script defer type="text/javascript">
    var modoShortcode = <?php echo $modoshortcode1 ?>;
    • This topic was modified 2 weeks, 1 day ago by snippet24.
Viewing 13 replies - 1 through 13 (of 13 total)
  • Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Rep

    From accessing variables from one file to another, you’ll have to use global to call the variables.

    <?php global $modoshortcode1, $paginashortcode1; ?>
    <script defer type="text/javascript">
    var modoShortcode = <?php echo $modoshortcode1; ?>;
    Thread Starter snippet24

    (@snippet24)

    @sterndata Thank you!!
    Question from this example https://developer.wordpress.org/reference/functions/wp_localize_script/#comment-1301
    I can define $translation_array in any regular template? I mean not functions.php but rather index.php

    @vijayhardaha
    Thank you.. Interesting, IIRC is using globals a practice rather avoided? on the good side seems pretty easy & simple solution

    • This reply was modified 2 weeks, 1 day ago by snippet24.
    • This reply was modified 2 weeks, 1 day ago by snippet24.

    putting things in index.php is a bad idea. if you need those variables just in the footer file then define there unless you really need those things in index.php

    Moderator bcworkz

    (@bcworkz)

    The localize script technique is tried and true, but it’s really a bit of a hack. It’s not intended for passing random vars, but for what it’s named for, localization. There is now a more appropriate function: wp_add_inline_script()
    https://developer.wordpress.org/reference/functions/wp_add_inline_script/
    It essentially does the same thing, it’s just better named 🙂

    Ensure $modoshortcode1 is within scope of whatever PHP is using it. Using globals is one way. Excessive use is discouraged, but if you need it you need it.

    Also check the page’s source HTML. It can happen that the JS assignment ended up after the script that uses it. When you use localize or inline script techniques dependencies are managed when the script is enqueued and you shouldn’t encounter such order of processing problems.

    Thread Starter snippet24

    (@snippet24)

    @bcworkz Thank you!!

    Well oddly even with:

    $modoshortcode1 = "lista";
    $paginashortcode1 = "7";
    global $modoshortcode1, $paginashortcode1;

    I keep getting the same error.

    putting things in index.php is a bad idea. if you need those variables just in the footer file then define there unless you really need those things in index.php

    mm maybe there’s a better way but I’m unaware of it.

    Moderator bcworkz

    (@bcworkz)

    Proper use of either the localize or inline script functions should address both issues. You can even conditionally enqueue the script so it only appears on pages that actually use it. The PHP for this is best placed in a child theme’s functions.php or a custom plugin you’ve created.

    @snippet24 you better provide the screenshots of your codes and errors. we can’t understand what you’re writing in your files.

    Thread Starter snippet24

    (@snippet24)

    Okey sure and I will give some context and code. If you still want the screenshots I can send them too but it I hope is better with the code below and description.

    I’m calling shortcodes with their attributes defined as variables before the shortcode execution.

    Like this:

    <?php
    
          $modoshortcode1 = "lista";
          $paginashortcode1 = "7";
          global $modoshortcode1, $paginashortcode1;
    
          $shortcodeactpasadas = '[actividades_shortcode pagina="'.$paginashortcode1.'" modo="'.$modoshortcode1.'" fecha="pasadas"]';
          echo do_shortcode($shortcodeactpasadas); ?>

    This code goes into several templates and has two variants for each template, each with different attributes (past and future activities for example.)

    Now there’s as well a script in footer.php which runs an ajax call after manipulating the passed php vartiables depending on which button was clicked.

    So only a part of it looks like this.

    var modoShortcode = console.log(*<?php echo $modoshortcode1 ?>*);
    var paginasShortcode = "1";
    var shortcodeAct = "";
    
    function cargarMasActFuncion() {
      paginasShortcode++;
      shortcodeAct = *[actividades_shortcode pagina="${paginasShortcode}" modo="${modoShortcode}" fecha="pasadas"]*;
      //console.log(shortcodeAct)
      var TipoFuncionAjax = "cargarMasPasadas";
      actionAjax(TipoFuncionAjax, shortcodeAct)
    }

    Edit: back tics replaced by *

    Do note I’m using a console.log for $modoshortcode1 in the script and that is what returns the error:

    <br />
    <b>Notice</b>:  Undefined variable: modoshortcode1 in <b>E:**footer.php</b> on line <b>164</b><br />

    Proper use of either the localize or inline script functions should address both issues.

    Okey I’m trying the inline script..

    • This reply was modified 1 week, 6 days ago by snippet24.
    • This reply was modified 1 week, 6 days ago by snippet24.
    • This reply was modified 1 week, 6 days ago by snippet24.
    • This reply was modified 1 week, 6 days ago by snippet24.
    Thread Starter snippet24

    (@snippet24)

    I apologize this is the full error

    var modoShortcode = "";
        modoShortcode = console.log(*<br />
    <b>Notice</b>:  Undefined variable: modoshortcode1 in <b>E:\U****path***\www\wordpress\wp-content\themes\otzerling\footer.php</b> on line <b>163</b><br />*);

    But I have no idea why the global fix doesn’t actually work..

    • This reply was modified 1 week, 6 days ago by snippet24.
    • This reply was modified 1 week, 6 days ago by snippet24.
    • This reply was modified 1 week, 6 days ago by snippet24.

    I am not sure what you’re trying to do what that javascript/jquery. Unable to understand your logic there. but here is information about the error that getting.

    In whatever you’re putting that PHP code it will be like this

    $modoshortcode1   = 'lista';
    $paginashortcode1 = '7';
    
    $shortcodeactpasadas = '[actividades_shortcode pagina="' . $paginashortcode1 . '" modo="' . $modoshortcode1 . '" fecha="pasadas"]';
    
    echo do_shortcode( $shortcodeactpasadas );

    and your footer php will have code like this

    <?php global $modoshortcode1, $paginashortcode1; ?> ?>
    <script>
    var modoShortcode = console.log('<?php echo $modoshortcode1; ?>');
    var paginasShortcode = "1";
    var shortcodeAct = "";
    
    function cargarMasActFuncion() {
      paginasShortcode++;
      shortcodeAct = *[actividades_shortcode pagina="${paginasShortcode}" modo="${modoShortcode}" fecha="pasadas"]*;
      var TipoFuncionAjax = "cargarMasPasadas";
      actionAjax( TipoFuncionAjax, shortcodeAct );
    }
    </script>

    Just a quick note, there is a better way to do things, but as I don’t understand your logic I can’t tell but you’re doing it seems wrong to me.

    Thread Starter snippet24

    (@snippet24)

    Ahhh okey so the “global string” should be in the footer, now it works 🙂 I was putting that line in “index.php”…

    Just a quick note, there is a better way to do things, but as I don’t understand your logic I can’t tell but you’re doing it seems wrong to me.

    perhaps here is a bit more explained?
    https://wordpress.org/support/topic/advice-on-building-an-events/

Viewing 13 replies - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.