Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7656301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:43:50+00:00 2026-05-31T12:43:50+00:00

I am facing a problem that I can not understand . During a plugin

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:43:52+00:00Added an answer on May 31, 2026 at 12:43 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing the problem that I cannot properly map my foreign key table
I'm facing a problem that seems to have no straighforward solution. I'm using java.util.Map
I'm working with Eclipse and ClearCase and we're facing the problem that there's no
I'm facing a problem while unit testing my forms. The problem is that data
That was helpful kgiannakakis. I'm facing a problem as below: a = ['zbc','2.3'] for
I am new to Python and Numpy, and I am facing a problem, that
I am facing problem with the control flajaxian.FileUploader i.e. after downloading the dll file
I am facing the issue.I changed the db.php file and unfortunately I put the
I'm facing a problem that I don't know how to solve and am hoping
I am using Eclipse Helios. Last days I am facing problem that Eclipse Helios

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.