Title: Global variable in wp_localize_script, help needed.
Last modified: August 22, 2018

---

# Global variable in wp_localize_script, help needed.

 *  Resolved [namchy](https://wordpress.org/support/users/namchy/)
 * (@namchy)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/global-variable-in-wp_localize_script-help-needed/)
 * It’s got me crazy! 🙂 What is wrong with my piece of code? wp_localize_script
   doesn’t recognize my variable, even I declare it global in my function…
    Thanks!
 *     ```
       $phpvariable = "This is from PHP";
   
       function js_enqueue_scripts() {
           global $phpvariable; 
           wp_register_script( "hendlerskripte", "/mojaskriptax.js" );
           wp_enqueue_script( "hendlerskripte" );
           wp_localize_script( "hendlerskripte", "JS_object_php_var",  $phpvariable);
       }
       add_action( "wp_enqueue_scripts", "js_enqueue_scripts" );
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 9 months ago](https://wordpress.org/support/topic/global-variable-in-wp_localize_script-help-needed/#post-10614502)
 * It must also be declared as global outside the function.
 *  Thread Starter [namchy](https://wordpress.org/support/users/namchy/)
 * (@namchy)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/global-variable-in-wp_localize_script-help-needed/#post-10614566)
 * Thank you for your reply.
    I thought this was a global variable outside my function,
   by definition
 * >  A variable declared outside a function has a GLOBAL SCOPE and can only be 
   > accessed outside a function
 *  :
 * `$phpvariable = "This is from PHP"; // global scope`
 * …but obviously I’m wrong. 🙂
    Please, a brief example how to do it would be great.
    -  This reply was modified 7 years, 9 months ago by [namchy](https://wordpress.org/support/users/namchy/).
    -  This reply was modified 7 years, 9 months ago by [namchy](https://wordpress.org/support/users/namchy/).
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 9 months ago](https://wordpress.org/support/topic/global-variable-in-wp_localize_script-help-needed/#post-10614580)
 *     ```
       global  $phpvariable; 
       $phpvariable = "This is from PHP";
   
       function js_enqueue_scripts() {
           global $phpvariable; 
           wp_register_script( "hendlerskripte", "/mojaskriptax.js" );
           wp_enqueue_script( "hendlerskripte" );
           wp_localize_script( "hendlerskripte", "JS_object_php_var",  $phpvariable);
       }
       add_action( "wp_enqueue_scripts", "js_enqueue_scripts" );
       ```
   
 *  Thread Starter [namchy](https://wordpress.org/support/users/namchy/)
 * (@namchy)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/global-variable-in-wp_localize_script-help-needed/#post-10614589)
 * Thank you dear sir, you saved my day!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Global variable in wp_localize_script, help needed.’ is closed to new
replies.

## Tags

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

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 2 participants
 * Last reply from: [namchy](https://wordpress.org/support/users/namchy/)
 * Last activity: [7 years, 9 months ago](https://wordpress.org/support/topic/global-variable-in-wp_localize_script-help-needed/#post-10614589)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
