-1

I have following chunk of wordpress code in footer which does not work, because global variables are not passed to function. Why?

function output_nav_link( $left ) {
    global $template_directory_uri_images, $current_page_menu_order, $nav_menu_items, $menu_items_count;

    $enabled = $left ? $current_page_menu_order > 1 : $current_page_menu_order < $menu_items_count;
    $output_link = '';
    $output_text = '';
    if ($enabled) {
        $nav_menu_item = $nav_menu_items[$current_page_menu_order - 1];
        $output_link = 'href="' . $nav_menu_item->url  . '"';
        $output_text = $nav_menu_item -> title;
    }
    ?>
    <div>
        <img src="<?= $template_directory_uri_images?>strelica_<?= $left ? 'lijevo' : 'desno'?>.svg" alt="<?= $left ? 'Prethodna' : 'Sljedeća'?> stranica">
        <div>
            <a <?= $output_link ?>><?= $output_text?></a>
        </div>
    </div>
    <?php
}


$template_directory_uri_images = get_template_directory_uri() . '/assets/images/';
$current_page_menu_order = get_post()->menu_order;
$nav_menu_items = wp_get_nav_menu_items('bocni');
$menu_items_count = count($nav_menu_items);

Edit: it seems it is an issue connected with my local configuration, which is wampserver, Apache 2.4.46 and PHP 7.4.10

Lovor
  • 236
  • 2
  • 9
  • Globals are passed to the function: http://sandbox.onlinephpfunctions.com/code/78ef4a670b2fde7ecc32f01b42a6d16025d4f96e I just set to the globals some values (because I didn't have the get-methods). – Sascha Sep 07 '20 at 19:39
  • It seems that it does not working in my development environment (WAMPServer, PHP 7.4.9, Apache 2.4.46) Is there some kind of switch to enable/disable globals? – Lovor Sep 07 '20 at 19:53

1 Answers1

0

Global DOES NOT make the variable global.

Global says that a local variable will be used as if it was a variable with a higher scope.

For more information: global variables in php not working as expected

Sascha
  • 4,576
  • 3
  • 13
  • 34
  • I read this article before asking this question. However it does not explain why this happens to me. It is all in the same scope and there are no additional include-s or require-s in the file. I would say that it is something due to my local environment setup, but I cannot figure it out what. In the meantime, I rewrote the code with passing parameter into functions, however I am curious - why it does not work? – Lovor Sep 07 '20 at 21:27