Title: global variables in theme&#039;s function.php
Last modified: August 21, 2016

---

# global variables in theme's function.php

 *  [geraldfuchs](https://wordpress.org/support/users/geraldfuchs/)
 * (@geraldfuchs)
 * [13 years ago](https://wordpress.org/support/topic/global-variables-in-themes-functionphp/)
 * 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

The topic ‘global variables in theme's function.php’ is closed to new replies.

## Tags

 * [function.php](https://wordpress.org/support/topic-tag/function-php/)
 * [global variable](https://wordpress.org/support/topic-tag/global-variable/)

 * 0 replies
 * 1 participant
 * Last reply from: [geraldfuchs](https://wordpress.org/support/users/geraldfuchs/)
 * Last activity: [13 years ago](https://wordpress.org/support/topic/global-variables-in-themes-functionphp/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
