0

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 var googledockey. 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.

<?php   
    if($_POST['gdocs2wp_hidden'] == 'Y') {  
        //Form data sent  
        $gdkey = $_POST['gdocs2wp_gdkey'];  
        update_option('gdocs2wp_gdkey', $gdkey);  


        ?>  
        <div class="updated"><p><strong><?php _e('Options saved.' ); ?></strong></p></div>  
        <?php  
    } else {  
        //Normal page display  
        $gdkey = get_option('gdocs2wp_gdkey');  
    }  
?>  

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 var googledockey is where I have had to manually save the key which works everytime. I have tried <?php echo $gdkey; ?> and get_option('gdocs2wp_gdkey'); 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

var jqueryNoConflict = jQuery;
//var googledockey = <?php echo $gdkey; ?>
var googledockey = "INSERTmyKEYhere";

// begin main function
jqueryNoConflict(document).ready(function(){

    initializeTabletopObject(googledockey);

});
4

4 回答 4

2

您总是可以让 JS 运行 Ajax 调用来获取数据。或者,您可以将变量声明移动到包含 JS 的 PHP/HTML 文件中,然后添加

<script type='text/javascript'> var googledockey="<?echo $gdkey;?>" </script>

于 2013-06-13T16:18:24.600 回答
1

1. 注册你的脚本

创建一个 JavaScript 文件,将其放在您的主题文件夹中,然后将其注册到 WordPress。

wp_register_script(
    'google-docs',
    get_bloginfo('template_directory') . '/scripts/google-docs.js'
);

文档:http ://codex.wordpress.org/Function_Reference/wp_register_script

2. 排队你的脚本

每当模板中需要您的脚本时,您就可以将文件排入队列。

wp_enqueue_script(
    'google-docs'
);

文档:http ://codex.wordpress.org/Function_Reference/wp_enqueue_script

3. 本地化你的脚本

这允许您在 JavaScript 中使用 PHP 变量。

wp_localize_script(
    'google-docs',
    'google_docs_vars',
    array(
        'key' => $google_doc_key
    )
);

文档:http ://codex.wordpress.org/Function_Reference/wp_localize_script

4. 在脚本中使用变量

现在您可以访问脚本中的变量。

var google_docs_key = google_docs_vars.key;

而已。我认为这将解决您的问题,这也是正确的方法。

于 2013-06-13T16:30:30.007 回答
0

JS 文件通常不会针对 PHP 进行解析。最简单(尽管不是最漂亮)的方法可能是:

1)在页面模板本身的隐藏DOM元素中回显值,然后使用JS抓取该元素并设置变量(因此,将值放在页面上的隐藏元素中,或者作为属性或其他东西,然后使用 JS 获取该值并将其分配给您的变量)。

2) 与上面类似,只需将变量声明放在一个内联脚本 ( <script>code</script>) 中,因为它将被解析为 PHP - 请参阅 Seriyia 的答案。

3) 使用简单的 AJAX 调用,这对 jQuery 来说非常简单,让您将数据从 JS 传递到其他地方的 PHP 函数functions.php,然后再传回 JS。如果您不熟悉 AJAX,这可能比它的价值更麻烦。

于 2013-06-13T17:44:43.423 回答
-1

这很简单您最近正在使用此代码片段

var jqueryNoConflict = jQuery;
//var googledockey = <?php echo $gdkey; ?>
var googledockey = "INSERTmyKEYhere";

// begin main function
jqueryNoConflict(document).ready(function(){

initializeTabletopObject(googledockey);

});

您需要用以下代码替换它的地方

var jqueryNoConflict = jQuery;
 var googledockey = "<?php echo $gdkey; ?>"
 var googledockey = "INSERTmyKEYhere";

   // begin main function
    jqueryNoConflict(document).ready(function(){

initializeTabletopObject(googledockey);

});
于 2013-06-13T16:29:07.490 回答