-1

I am making a WordPress plugin and some of the options from the admin interface are for the JavaScript file that handles the interface of the widget.

The issue is that I do not see a practical way to echo the settings into that file. I was considering making a PHP file that fakes its file content type. Or creating a dynamic JavaScript file whenever the page loads, but currently im sharing the options through a hidden div that has the information as a base64 encoded json string that is read and loaded when the page finishes loading.

What is the best way to set my WordPress settings into my JavaScript file?

4

1 回答 1

0

将设置填充到数组中,然后使用 json_encode() 将它们转换为 JS 数组结构:

一些脚本.php:

<?php

$settings = array(
   'x' => 'foo',
   'y' => 'bar'
);

header('Content-type: text/javascript');
echo 'var settings = ' . json_encode($settings);

然后将其加载到您的客户端 html 中:

<script src="somescript.php" type="text/javascript"></script>
于 2012-06-10T18:47:22.067 回答