I'm working on a Wordpress plugin. I need to pass plugin directories (that can change depending on an individual's installation) to a jquery function.
我正在开发Wordpress插件。我需要将插件目录(根据个人的安装可以更改)传递给jquery函数。
What is the best way of doing this? The version of the plugin that I can to work on had included all the javascript in the PHP file so the functions were parsed along with the rest of the content before being rendered in a browser.
最好的方法是什么?我可以处理的插件版本包含PHP文件中的所有javascript,所以在浏览器中呈现之前,函数和其他内容一起被解析。
I'm looking at AJAX but I think it might be more complicated than I need. I can get away with just two variables in this case (directories, nothing set by the user).
我正在研究AJAX,但我认为它可能比我需要的要复杂。在这种情况下,我可以只使用两个变量(目录,用户不设置)。
As I've read its good practice, I'm trying to keep the js and php separate. When the plugin initializes, it call the js file:
正如我所看到的它的良好实践一样,我试图将js和php分开。当插件初始化时,它调用js文件:
//Wordpress calls the .js when the plugin loads
wp_enqueue_script( 'wp-backitup-funtions', plugin_dir_url( __FILE__ ) . 'js/wp-backitup.js', array( 'jquery' ) );
Then I'm in the .js file and need to figure out how to generate the following variables:
然后我在.js文件中,需要知道如何生成以下变量:
dir = '<?php echo content_url() ."/plugins"; ?>';
dir = '<?php echo content_url() ."/themes"; ?>';
dir = '<?php echo content_url() ."/uploads"; ?>';
And run the parse the following requests:
并运行以下请求的解析:
xmlhttp.open("POST","<?php echo plugins_url() .'/wp-backitup/includes/wp-backitup-restore.php'); ?>",true);
xmlhttp.open("POST","<?php echo plugins_url() .'/wp-backitup/includes/wp-backitup-start.php'); ?>",true);
xmlhttp.open("POST","<?php echo plugins_url() .'/wp-backitup/wp-backitup-directory.php'); ?>",true);
xmlhttp.open("POST","<?php echo plugins_url() .'/wp-backitup/wp-backitup-db.php'); ?>",true);
window.location = "<?php echo plugins_url() .'/wp-backitup/backitup-project.zip'); ?>";
xmlhttp.open("POST","<?php echo plugins_url() .'/wp-backitup/wp-backitup-delete.php'); ?>",true);
Content URL and Plugins URL differ only by /plugins/ so if I was hard pressed, I would only really need to make a single PHP request and then bring this into the JS.
内容URL和插件URL仅因/ Plugins /而不同,所以如果我被硬按了,我只需要发出一个PHP请求,然后将它带到JS中。
3 个解决方案
#1
1
Quick and dirty:
快速和肮脏的:
<script type="text/javascript">
<?php
echo 'var myJsObject =' . json_encode(array(
'dir1' => $directory1,
'dir2' => $directory2
));
?>
alert(myJsObject.dir1);
</script>
#2
1
Your function can be loaded in a .js file, but the PHP variables you need can be given when you itiniatlize the plugin in a PHP page. By exemple:
函数可以在.js文件中加载,但是当在PHP页面中对插件进行itiniatlize时,可以给出所需的PHP变量。为例:
$("#myDiv").startPlugin("<?php echo $directory;?>");
If you need informations from php, you don't really have the choice to give it to the plugin somewhere. Querying it with ajax wouldn't be a good idea as of me.
如果你需要来自php的信息,你没有选择把它交给插件。用ajax查询它对我来说不是一个好主意。
#3
1
Its kind of hard without seeing the code but generally speaking i would ouput the directories somewhere from the php like:
如果没有看到代码就很难,但一般来说,我会把目录从php中删除,比如:
<script type="text/javascript">
var dirs = <?php echo empty($dirs) ? "{}" : json_encode($dirs); ?>;
</script>
#1
1
Quick and dirty:
快速和肮脏的:
<script type="text/javascript">
<?php
echo 'var myJsObject =' . json_encode(array(
'dir1' => $directory1,
'dir2' => $directory2
));
?>
alert(myJsObject.dir1);
</script>
#2
1
Your function can be loaded in a .js file, but the PHP variables you need can be given when you itiniatlize the plugin in a PHP page. By exemple:
函数可以在.js文件中加载,但是当在PHP页面中对插件进行itiniatlize时,可以给出所需的PHP变量。为例:
$("#myDiv").startPlugin("<?php echo $directory;?>");
If you need informations from php, you don't really have the choice to give it to the plugin somewhere. Querying it with ajax wouldn't be a good idea as of me.
如果你需要来自php的信息,你没有选择把它交给插件。用ajax查询它对我来说不是一个好主意。
#3
1
Its kind of hard without seeing the code but generally speaking i would ouput the directories somewhere from the php like:
如果没有看到代码就很难,但一般来说,我会把目录从php中删除,比如:
<script type="text/javascript">
var dirs = <?php echo empty($dirs) ? "{}" : json_encode($dirs); ?>;
</script>