Title: Setting JavaScript variables
Last modified: August 18, 2016

---

# Setting JavaScript variables

 *  [markwheeler](https://wordpress.org/support/users/markwheeler/)
 * (@markwheeler)
 * [19 years ago](https://wordpress.org/support/topic/setting-javascript-variables/)
 * Is there any way to set JavaScript variables in an external .js file (ie, edit/
   generate an external text file) from a WordPress plugin?
 * Or is the only solution to set them in a script tag in the head of every page?
   Which obviously seems a little ‘clunky’ 😉
 * Cheers,
 * Mark

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

 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [19 years ago](https://wordpress.org/support/topic/setting-javascript-variables/#post-561114)
 * yes. and no.
 *  Thread Starter [markwheeler](https://wordpress.org/support/users/markwheeler/)
 * (@markwheeler)
 * [19 years ago](https://wordpress.org/support/topic/setting-javascript-variables/#post-561128)
 * Would you care to share a little more than that? How DO you set variables in 
   a js file?
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years ago](https://wordpress.org/support/topic/setting-javascript-variables/#post-561131)
 * > _Is there any way to set JavaScript variables in an external .js file (ie, 
   > edit/generate an external text file) from a WordPress plugin?_
 * Have the plugin insert a `<script src="url-to-plugin?get_script=1">` tag into
   the page to make a script call to itself, then have it check for calls with a`
   $_GET["get_script"]` variable to make it generate the necessary javascript code.
 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [19 years ago](https://wordpress.org/support/topic/setting-javascript-variables/#post-561132)
 * I am assuming you are interested in using internal WP variables?
 * I’ve managed to do it the same way if I were simply using js within `<script>`
   tags
 *     ```
       function blah()
       {
       	var thing = prompt('blahblahblah:', '<?php if (empty($paged)) { echo "1";} else { echo $paged; } ?>'); ... and so on
       ```
   
 * You just need to enclose php properly.
 *  Thread Starter [markwheeler](https://wordpress.org/support/users/markwheeler/)
 * (@markwheeler)
 * [19 years ago](https://wordpress.org/support/topic/setting-javascript-variables/#post-561274)
 * Cheers, just using PHP directly in the script file will probably be best. But
   how do I go about including the appropriate WordPress functions so I can use 
   get_option() to read from the database?
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years ago](https://wordpress.org/support/topic/setting-javascript-variables/#post-561278)
 * markwheeler: `require_once('../../../wp-blog-header.php');`
 *  Thread Starter [markwheeler](https://wordpress.org/support/users/markwheeler/)
 * (@markwheeler)
 * [19 years ago](https://wordpress.org/support/topic/setting-javascript-variables/#post-561280)
 * Thanks very much guys 🙂
 *  [mike_l](https://wordpress.org/support/users/mike_l/)
 * (@mike_l)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/setting-javascript-variables/#post-561392)
 * HOLY CRAP… I’ve been looking for wp-blog-headers for at least 3 hours. Thanks
   guys.

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

The topic ‘Setting JavaScript variables’ is closed to new replies.

## Tags

 * [edit](https://wordpress.org/support/topic-tag/edit/)
 * [external](https://wordpress.org/support/topic-tag/external/)
 * [file](https://wordpress.org/support/topic-tag/file/)
 * [generate](https://wordpress.org/support/topic-tag/generate/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [js](https://wordpress.org/support/topic-tag/js/)

 * 8 replies
 * 4 participants
 * Last reply from: [mike_l](https://wordpress.org/support/users/mike_l/)
 * Last activity: [18 years, 1 month ago](https://wordpress.org/support/topic/setting-javascript-variables/#post-561392)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
