WordPress.org

Forums

global variables in theme's function.php (1 post)

  1. geraldfuchs
    Member
    Posted 2 years ago #

    Hi,
    I am writing a customer theme, and want to use a global variable in the function.php.
    It looks like the global variable is loosing it value which is set in a filter function.

    How can I achieve that the value, which is set in the filter function is available in the function.php?

    Here is the code of my function.php showing the issue I have.

    <?php
    
    $myGlobalVar;
    print_r($myGlobalVar);
    
    add_theme_support('menus');
    
    //register menue locations
    function register_my_menus() {
        global $helperClass;
        register_nav_menus(
                array('top-menu' => __('Top Menu'),
                    'left-menu' => __('Left Menu'))
        );
    }
    
    add_action('init', 'register_my_menus');
    
    // add hook
    add_filter('wp_nav_menu_objects', 'my_wp_nav_menu_objects_sub_menu', 10, 2);
    
    // filter_hook function to react on sub_menu flag
    function my_wp_nav_menu_objects_sub_menu($sorted_menu_items, $args) {
        global $myGlobalVar;
    
        $myGlobalVar = array("this is a global var", "This as well");
    
        return $sorted_menu_items;
    }
    
    ?>

    thanks to all of you for helping me here.
    Gerald

Topic Closed

This topic has been closed to new replies.

About this Topic