I would like to get relative path from a php file without knowing the absolute path. I have succeeded to do it but for a reason which I don't know, on some servers (in rare case) it doesn't work when I enqueue css/js. Slashes are missing...

Here the code:

define('PATH', trailingslashit(str_replace('\\', '/', dirname(__FILE__))));
define('URI', site_url(str_replace(trailingslashit(str_replace('\\', '/',ABSPATH)), '', PATH)))

What did I miss? Does it come from the server or the code itself?


2 Answers 2


Just don't do it, you can never know if the plugins directory is even "below" ABSPATH, and you can not know the URL out of the directory path https://codex.wordpress.org/Determining_Plugin_and_Content_Directories.

Just use the core api of plugins_utl etc, don't reinvent the wheel.

  • Thank you for your answer, but there isn't any native Wordpress function that will match my needs. In fact, the file will not be loaded directly from the root folder. I build a metabox framework which can be included in any plugin or theme. So, it must automatically fetch the right path without knowing if its a theme/plugin and where is the framework folder in the theme/plugin. The aim is to use url/path to enqueue/require/include.
    – freaky
    Commented Nov 28, 2015 at 19:48
  • and that is exactly why you should use the core api instead of reinventing things, and if there is something that cab bot be done with core api then it is very likely it is not worth being done at this point in time for your specific use case. Commented Nov 28, 2015 at 20:14
  • And do you have an example of how to make it work? Because I don't know how to use a native Wordpress function to handle what I want to do. It seems that all frameworks are using php functions and not Wordpress functions because it seems not possible... If have a working example, I will be great! With plugin_dir_url it's not working, still the same issue on some server...
    – freaky
    Commented Nov 28, 2015 at 20:17
  • it sounds like what you want to do is fixed easily but having some initialization api that will let your code know where it is installed. Commented Nov 28, 2015 at 20:20
  • Of course, but I don't want to defined a constant before, to run correctly the framework. Eventually, if I can detect if the current file is from a plugin or a theme then i could use plugin_dir_url and plugin_dir_path...
    – freaky
    Commented Nov 28, 2015 at 20:24
function get_url_from_path ($path) {

    // clean paths
    $dir_path = str_replace( '\\', '/', $path ) ); // file or directory
    $site_dir = trailingslashit( str_replace( '\\', '/', ABSPATH ) ); // path set in wp-config

    // remove server directory from path
    $file_rel = str_replace( $site_dir, '', $dir_path );

    // convert relative url to absolute url
    $dir_rel_url = site_url ($file_rel, 'http' );

    // return
    return $dir_rel_url;    

// __FILE__ and __DIR__ are referencing the php calling these functions

echo get_url_from_path (__FILE__);

echo trailingslashit( get_url_from_path (__DIR__) );
  • Thank you for your answer, but you did near the same thing. it doesn't solve the issue...
    – freaky
    Commented Nov 28, 2015 at 19:58
  • Maybe show a few examples of files and how you expect the url to end up. It would be pretty easy to find a solution with more information. Also, maybe where you are expecting to call this function because FILE and DIR will change.
    – jgraup
    Commented Nov 28, 2015 at 20:05
  • I use it, for example, with wp_enqueue_style('css', URI .'assets/css/file.css');. In rare case, on some server, the slashes are completely missing and then the file is not enqueued because the url became false.
    – freaky
    Commented Nov 28, 2015 at 20:09
  • trailingslashit ( URI ) will make sure there is always a '/' at the end. But if your FILE and DIR are missing slashes, that has nothing to do with WordPress. Actually, your whole question really isn't WordPress related. What are you getting on these bad servers from FILE? And is there something in $_SERVER or $_REQUEST that shows the correct path?
    – jgraup
    Commented Nov 28, 2015 at 20:25
  • It was mentionned in my question...
    – freaky
    Commented Nov 28, 2015 at 20:26

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.