Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a file that can either be included in a plugin, or in a theme. What's the best way to get the URL of the file's folder (from within the file)?

Edit:

I'm now using this for now

home_url( '/' . str_replace( ABSPATH, "", dirname( __FILE__ ) ) );

Let me know if there's any problem with this / there's a better way.

share|improve this question
    
Do you know the exact location within the Plugin where the file would be located? –  Chip Bennett Mar 15 '12 at 18:16
    
What does this file do? There might be a better way ... And do you need the full URL or to just know the location? –  EAMann Mar 15 '12 at 18:19
    
@ChipBennett Nope, I don't know anything about the relative location of the file in either the plugin or the theme. –  Dogbert Mar 16 '12 at 5:01
    
@EAMann It's an Options page generator/helper, that could either be used by a plugin or a theme. –  Dogbert Mar 16 '12 at 5:02
2  
Since this is not quite trivial task for generic case, could you add some details how youe envision your library will be used and included in third party code? –  Rarst Mar 16 '12 at 11:34
add comment

5 Answers

Using ABSPATH and home_url() might not work if the wp installation is in a different directory to the url it is displayed at. You should test that out.

My thought is using the content directory as a place to do the replacement might be more robust as you can pass the resulting path into content_url() which accounts for where the WP installation is:

function get_file_url( $file = __FILE__ ) {
    $file_path = str_replace( "\\", "/", str_replace( str_replace( "/", "\\", WP_CONTENT_DIR ), "", $file ) );
    if ( $file_path )
        return content_url( $file_path );
    return false;
}

This could be simplified for unix only but the above supports windows too.

share|improve this answer
    
Note that it's not guaranteed that plugin/theme is actually inside content dir. They can be easily located elsewhere. –  Rarst Mar 15 '12 at 18:11
    
Ah good point. Still, 80/20 ;) –  sanchothefat Mar 16 '12 at 9:54
    
@Rarst What exactly are you referring too? –  kaiser Mar 16 '12 at 11:24
    
@kaiser theme and plugin directories can be configured, residing inside of wp-content is merely default setting for them. –  Rarst Mar 16 '12 at 11:33
    
@Rarst Ah, sry. Missread. Thought it was WP_PLUGIN_DIR. –  kaiser Mar 16 '12 at 11:38
add comment

See Determining Plugin and Content Directories.

plugins_url( 'filename', __FILE__ );

… returns the full URI to the file in your plugin. For themes you use:

get_stylesheet_directory_uri() . '/path/to/file';
share|improve this answer
    
The problem is, that I don't know if it's included in a plugin or a theme. Also, I don't know the path of the file, so your stylesheet example won't work. Neither do I know the name of the plugin. It's a utility library. –  Dogbert Mar 11 '12 at 6:01
1  
I write my own library right now, and it is a plugin. Themes and other plugins may use it, but the library is kept separate. Otherwise there would be no way to create a reliable file access – as you can see. :P –  toscho Mar 11 '12 at 7:07
add comment

I suggest you use a filter.

In your file, somewhere at the very beginning, include something along the lines of:

add_filter( 'wpa45165_resource_url', 'wpa45165_resource_url_finder' );
function wpa45165_resource_url_finder( $url ) {
    $url = /* Some code to generate the url from WP_SITEURL and dirname() */;

    return $url;
}

Then, in the code that needs to reference the file, apply the filter to an empty string:

$include_url = apply_filters( 'wpa45165_resource_url', '' );
share|improve this answer
add comment

Why won't you write both case and check which one is actually pointing to a real file:

function dogbert_library_get_file_path($filename) {
    // As you might put your file in a folder, just configure this path
    $path_to_file = "files/" .$filename;

    // get_theme_root() doesn't give trailing slash
    $themeFile = get_theme_root() ."/" .get_current_theme() ."/" .$path_to_file;
    // plugin_dir_path does give trailing slash
    $pluginFile = plugin_dir_path( __FILE__ ) .$path_to_file ;

    // As we are at filesystem layer, we can use file_exists to check
    if(file_exists($themeFile)){
        return $themeFile;
    } else if(file_exists($pluginFile)) {
        return $pluginFile;
    } else {
        // Return empty as a fallback...
       return "";
    }
}

That's the best option I would see.

share|improve this answer
add comment
$path = dirname(__FILE__);

should do the trick.

share|improve this answer
3  
An URL is not the local path. –  toscho Mar 18 '12 at 9:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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