• Resolved AndySwede

    (@andyswede)


    “I have code that works normally in a template but doesn’t work when in the widget?

    Code in a template runs in the global context. Code in the widget will run in a function context. Make sure that you declare any global variables as global before attempting to use them”

    What does that mean?
    How do i configure the code below to work in the widget?

    <?php $lunch = get_group('lunch');  foreach($lunch as $lunch){
    echo '<span class="lunch-dag">' . $lunch['lunch_dag'][1]. '</span><br />';
    echo '<span class="lunch-matratt">' . $lunch['lunch_matratt'][1]."<br />";
    echo $lunch['lunch_matratt'][2]."<br />";
    echo $lunch['lunch_matratt'][3]."<br />";
    echo $lunch['lunch_matratt'][4]. '</span><br /><br />';}?>

    Best regards Andy

    https://wordpress.org/plugins/php-code-widget/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Well, what it means, ultimately, is not to ask me to fix your broken PHP code. As much as I want to provide support, I’m really not interested in providing support for every little PHP problem you can possibly have.

    For example, I could point out that this PHP code: foreach($lunch as $lunch) makes no actual sense whatsoever. You can’t make a loop and reset your array variable as part of that loop and expect things to work properly.

    But the thing is that I really shouldn’t be asked to do that. If you can’t write proper PHP code that works, then you shouldn’t be using this widget. If you don’t know the difference between the global context and the function context, then you shouldn’t be using this widget.

    That’s really what it means, in the long run. Best of luck to you though.

    Thread Starter AndySwede

    (@andyswede)

    How can someone like you even survive as a human being?
    The most rude person i have ever seen in a forum!
    Luckily i had an private answer from a friend and it was very easy to resolve.

    Just one question to you Mr.Otto, how come that code works fine in a template and does the job if it is totally wrong??

    Thanks for nothing!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘What does it mean?’ is closed to new replies.