Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to run php code within a Javascript (.js) file
    text
    copied!<p>First off I am very new to php so bear with me. I have a javascript (.js) file from a wordpress template that reads the key from <code>var googledockey</code>. In order to change it I have to manually open the .js file and change that variable. What I would like to do is have the .js file grab the key from where it was saved on a page I made. Here is the code for the admin page that has the textfield for me to enter in a key.</p> <pre><code>&lt;?php if($_POST['gdocs2wp_hidden'] == 'Y') { //Form data sent $gdkey = $_POST['gdocs2wp_gdkey']; update_option('gdocs2wp_gdkey', $gdkey); ?&gt; &lt;div class="updated"&gt;&lt;p&gt;&lt;strong&gt;&lt;?php _e('Options saved.' ); ?&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt; &lt;?php } else { //Normal page display $gdkey = get_option('gdocs2wp_gdkey'); } ?&gt; </code></pre> <p>The key saves and whenver I open the page they key shows up so I know this half is working. This is where I am stumped. Within my .js file which is in a subdirectory of the admin page, the <code>var googledockey</code> is where I have had to manually save the key which works everytime. I have tried <code>&lt;?php echo $gdkey; ?&gt;</code> and <code>get_option('gdocs2wp_gdkey');</code> to try and get the key but I havent had any luck. Can php work within a .js file? Does anyone have any insight to help me along? Thanks</p> <pre><code>var jqueryNoConflict = jQuery; //var googledockey = &lt;?php echo $gdkey; ?&gt; var googledockey = "INSERTmyKEYhere"; // begin main function jqueryNoConflict(document).ready(function(){ initializeTabletopObject(googledockey); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload