First, let me tell you how I'm currently doing it. I have this function. It's purpose is to take the the string of a file system path (/root/web/dir/some/path/to/file) and return the equivalent URL (http://mydomain.com/some/path/to/file). In order for this function to work properly, it needs to know where the root web directory (RWD) is on the system. It does this by assuming $_SERVER['DOCUMENT_ROOT'] as the RWD.

function path_to_url($path)
    $path = str_replace('\\', '/', $path);
    $doc_root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);

    return 'http://'.$_SERVER['HTTP_HOST'].'/'.str_replace(' ', '%20', str_replace($doc_root, '', $path));

Please learn this function and know what it does in order to understand what I'm talking about in this post.

With this function, all I would need to know in order to include a script/stylesheet on my page is it's path. I would be writing code that looks something like:

wp_enqueue_script('myscript', path_to_url(dirname(__FILE__).'/script.js'));

This worked fine, in fact, it worked so well that I started to use path_to_url in a lot of places in my theme.

There's one reason why this solution doesn't work in some circumstances. Some shared hosting sites (bluehost) don't have $_SERVER['DOCUMENT_ROOT'] set to the RWD as I expected and assumed it to be. Bluehost seems to always have it set to "/usr/local/apache/htdocs" which does not correlate with the paths I use within PHP -- "/home1/demo/public_html/wp-content/themes/...". So I must either give a work around to those most unfortunate to have $_SERVER['DOCUMENT_ROOT'] set to something as incorrect (at least to my knowledge of it seeming to be incorrect) as the above, or I should figure out another means of doing what I want/need to do.

The solution would be to somehow correct $_SERVER['DOCUMENT_ROOT']. But this doesn't seem like a valid solution for my customers. They would need to submit a support ticket notifying me that my theme isn't working. Then I would need to reply with a message on how they can fix the issue. But before I could simply reply to them with a solution, I will need to know their RWD somehow. This would be a very cumbersome solution and would require more time and effort then seems practical.

So I'm left with the second solution -- figure out another means of doing what I need to do. While writing this I figured that I could go with the hard-coded approach, and maybe that's the only way to go about this problem.

wp_enqueue_script('script', get_bloginfo('template_url').'/lib/framework/panel/script.js');

After all my explanation, this seems like a no-brainer. But the only quarrel I have with this is how rigid it is. For example, if I decide to move my script.js file around, I'll need to edit this code, no matter what. What I like about the above path_to_url solution, is that if I moved the script.js and the php template file, nothing breaks; so long as the script.js file stays in the same folder as the php template file. In fact, what I was doing with the path_to_url solution is making things relative again, relative to the php template file!

Unfortunately though, I realize that it is not practical to expect there's one formula for converting all file system paths on all systems to URLs. This is due to the fact that there are different server configurations that make it impossible for one formula to know the RWD in a path. For example, one server could have a setup where wordpress is installed under http://example.com/~demo/ and the RWD would be /home1/demo/public_html/. Thus there's no way I can get the RWD our of __FILE__ with this kind of setup. How could I?

I hope you found this post/question interesting. I'll leave you with this question: How can I make wp_enqueue_script('script', get_bloginfo('template_url') . '/lib/framework/panel/script.js'); more relative and less rigid?

Thanks guys. Peace!

  • No, I don't think this is possible. You just can't make everything relative, suppose if you could make it work then you will have to change the path to the PHP script when you move php script and .js together. The thing is if you are shipping them with your theme or plugin then you can use the wp provided methods to get the path to the plugin/theme directory and you will be good to go, as you have already demonstrated using get_bloginfo. May 15, 2011 at 6:00

1 Answer 1


Do this at the head of your functions.php:

define('PATH_TO_URL', get_bloginfo('template_url') . [path to your libraries, etc.]);

Then, just call wp_enqueue_script('script', PATH_TO_URL . 'myscript.js'); or similar anywhere you want. If you change the location, just change the define() and it'll update everywhere.

Easy peasy.

  • Not all my files are in the same path though. hmm
    – Sam
    May 15, 2011 at 5:46
  • Here's roughly my directory layout: lib[framework[panel[setup.php, script.php, style.css], betabin[betabin.js, setup.php, betabin.css], glorious[setup.php, glorious.js, ...], setup.php, utilities.php, widgets.php]]. The words with [] after them mean they're directories contianing whatever is in between those brackets. So with this, you can see all my js and css files are each in their own directories. I could maybe make a PATH_TO_FRAMEWORK constant that shortens "/lib/framework/panel/style.css" to PATH_TO_FRAMEWORK."/panel/style.css", which isn't really shortened, and that's not the goal here.
    – Sam
    May 15, 2011 at 5:55
  • The goal is to make a script that dynamically generates these URLs relative to the template file. In other words, if the PHP template file moves, then the URLs would change. This means that the js and css files could move freely so long as the php template file moves with them, and visa versa.
    – Sam
    May 15, 2011 at 5:58
  • Then let me ask: what is your goal here? Perhaps I'm misunderstanding what you're getting at.
    – ZaMoose
    May 15, 2011 at 14:42
  • I have to question one of your underlying assumptions: in the context of a WordPress Theme, why would the PHP template file ever move? Jul 14, 2011 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.