6

I am facing a problem that I can not understand .

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__)) ?>";

Results :

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.. "

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

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 )

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

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 ?

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

EDIT I :

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');

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 ?

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

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.

Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • 1
    Don't you need to `include` or `require` some file(s) before you could reference wordpress functions/constants in your app? – Salman A Mar 14 '12 at 06:42
  • good question.. exactly what I would like to know . and if so - which ones ?? – Obmerk Kronen Mar 14 '12 at 06:44
  • this will help you . - http://codeblow.com/questions/php-within-js-file-wordpress/ - http://stackoverflow.com/questions/6260902/how-to-pass-variable-from-php-template-to-javascript - http://stackoverflow.com/questions/6401297/php-constants-in-a-javascript-file - http://stackoverflow.com/questions/6808221/php-within-js-file-wordpress – Milap Mar 14 '12 at 06:34

1 Answers1

4

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__ does not need to access anything from Wordpress, which is why it works as expected.

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.

Edit

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');

Source

Community
  • 1
  • 1
Matt Beckman
  • 5,022
  • 4
  • 29
  • 42
  • this is understood from the question itself :-) . the real question is WHY , and HOW to make it work. I can not see a real reason for referencing any particular thing - being that tha JS is called BY wp - and from within wp functions . Can you maybe elaborate on what is exactly the problem and how to resolve it ? – Obmerk Kronen Mar 14 '12 at 06:43
  • 1
    You are calling a JavaScript file that is built by PHP. However, the call is coming from your browser directly to that file, and it is *not* coming from the Wordpress PHP pipeline. When using these type of PHP-built JavaScript files, you need to reference all required files in the PHP file itself. – Matt Beckman Mar 14 '12 at 06:46
  • Example: if you browse to http://www.example.com/file.js.php in your browser, this will never even touch Wordpress unless you *explicitly* tell it to (using `include` or `require`). Other Wordpress plugins are included via `index.php`, and as such already have all the required references. – Matt Beckman Mar 14 '12 at 06:49
  • well - I thought that being called from "enqueue_script" and "register_script" does make it a part of the wp pipline . it is being included using the init() function. but anyhow - if that is indeed the case - what are the files do I need to include ?? – Obmerk Kronen Mar 14 '12 at 06:51
  • your latest update gives me another error : `failed to open stream` .. I must also stress that there is absolutly no other problem in the server or website or any of my other plugins on the same location. the problems are only when using JS/PHP combined file. – Obmerk Kronen Mar 14 '12 at 06:58
  • 2
    Try hard coding the path to `wp-load.php` file like so: `include('/var/www/whatever/somedomain.com/wordpress/wp-load.php');` – Salman A Mar 14 '12 at 07:22
  • @Matt Beckman - as for your comments #2 - I assumed my plugin is included via the index.php as it uses the plugins load mechanism just like any other plugin. – Obmerk Kronen Mar 14 '12 at 09:00