JS文件中的PHP常量 [英] PHP constant inside JS file

查看:177
本文介绍了JS文件中的PHP常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个我无法理解的问题。

I am facing a problem that I can not understand .

在插件开发过程中,我包含了一个file.js.php(register / enqueue)。

During a plugin development I am including a file.js.php (register/enqueue).

<?
/* 
File.js.php
*/
Header("content-type: application/javascript");
$path =  constant('WP_PLUGIN_DIR'); //test with function
$path_2 =  WP_PLUGIN_DIR; // test directly
//can cause a problem with older browsers?? use text/javascript
?>
//////////////////// Begin Tests  ////////////
var templateDir = "<?php echo WP_PLUGIN_URL ?>" ;
var templateDir2 = "<?php echo $path ?>" ;
var templateDir3 = "<?php echo $path_2 ?>" ;
var templateDir4 = "<?php echo constant("WP_PLUGIN_URL") ?>";
var templateDir5 = "<?php echo __FILE__ ?>";
var templateDir6 = "<?php echo plugins_url( 'somedir/somefile.png' , dirname(__FILE__)) ?>";

结果:

var templateDir = "WP_PLUGIN_URL" ; // simply outputs a string of the constant name
var templateDir2 = "" ; // null or empty
var templateDir3 = "WP_PLUGIN_DIR" ;// simply outputs a string of the constant name
var templateDir4 = "//Warning:  constant()  Couldn't find constant WP_PLUGIN_URL in .."
var templateDir5 = "path.to.js.php" // only one that works ;
var templateDir6 = "Call to undefined function  plugins_url() in.. "

所以我的测试向我显示MAGIC CONSTANTS有效,但任何WP常数都不可用。

so my tests showed me that MAGIC CONSTANTS work , but any WP CONSTANT will be unavailable .

包括在plugin.php中声明的MY OWN常量(实际上这就是我开始测试WP常量的原因)

That includes MY OWN constants that were declared in the plugin.php (actually this is the reason why I even began testing the WP constants )

有趣的是 - 不仅CONSTANTS不可用 - 而且任何wp函数返回不可用。

Interesting enough - not only CONSTANTS are unavailable - but any wp function is returning "unavailable" .

PHP常量通过应用程序随时可用..
这是WP特定的问题吗?这是故意的吗?或者我做错了什么?

PHP constant are meant to be available at all times through the app.. Is that a WP specific problem ? Is that intentional ? or am I doing something wrong ?

注意:我知道还有其他方法可以做到这一点(比如使用localize_script将变量传递给JS - 或者只是使用一个函数来输出标题中的路径) - 但首先 - 这些方法对我来说并不理想 - 更重要的是,我想了解为什么这种方法失败...

NOTE : I know there are other ways to do it (like using localize_script to pass variables to JS - or just use a function to output the path in the header) - but first - those methods will not be ideal for me - and more importantly is the fact that I want to understand why this method is failing ...

编辑I:

Althouh @Matt Beckman指出正确的方向他的具体方法不起作用。
确实必须包含来自WP的文件。
对我来说,以下工作:

Althouh @Matt Beckman pointed in the right direction his specific method did not work. It is true that a file from WP must be included . For me both the following work :

include("../../../../wp-load.php");
require_once (dirname(dirname(dirname(dirname(dirname ( __FILE__))))).'/wp-load.php');

你可以想象两者都是平等的 - 但问题仍然存在:那些有点硬编码(如@Salman A)建议 - 如果插件的目录发生变化怎么办?在那种情况下解决方案是什么?

Both as you can imagine are equal - but the problem remains : those are somewhat Hardcoded (like @Salman A) suggested - what if the plugin´s dir changes ?? what is the solution in that case ?

请注意, wp-load.php wp-config.php 为我工作。我不知道什么是更好或哪些可以提出一些安全问题。

Note that the both wp-load.php and wp-config.php worked for me . I do not know what is better or which can present some security issues.

但我想这是另一个问题..

but I guess it is for another question ..

底线:这个解决方案只有TEMP,直到我会找到正确的答案。我的插件是通过WORDPRESS插件mechanisem(enqueue_script()/ register_script()/ init()等来加载的。) - 因此我无法理解它为什么这样做。现在它的工作原理如上所述。

Bottom line : this solution is only TEMP until I will find the right answer . My plugin is loading via the WORDPRESS plugin mechanisem (enqueue_script() / register_script() / init() etc.. ) - and therefor I can not grasp why it does so.. Bu for now it works like described above.

推荐答案

因为它包含在JavaScript中,你的 file.js.php 需要引用Wordpress库才能访问这些常量。目前,正如您的代码所代表的那样,它没有任何对这些常量的引用。

Being that this is being included like JavaScript, your file.js.php needs to reference the Wordpress library in order to access those constants. Currently, as your code stands, it does not have any references to those constants.

__ FILE __ 不需要从Wordpress访问任何东西,这就是它按预期工作的原因。

__FILE__ does not need to access anything from Wordpress, which is why it works as expected.

你需要包含一些 require include 语句引用 file.js.php 顶部所需的特定Wordpress PHP文件。

You will need to include some require or include statements referencing the specific Wordpress PHP files you need at the top of file.js.php.

编辑

使用file.js.php文件顶部的以下内容进行访问这些常数:

Use the following at the top of your file.js.php file to get access to those constants:

$home_dir = preg_replace('^wp-content/plugins/[a-z0-9\-/]+^', '', getcwd());
include($home_dir . 'wp-load.php');

来源

这篇关于JS文件中的PHP常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆