Get URL of a specific file

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:

Read More

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.

Related posts

Leave a Reply

6 comments

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

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

  3. 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', '' );
    
  4. TL;DR

    $themes_search_index = strpos(__FILE__,'wp-content/themes');
    $plugins_search_index = strpos(__FILE__,'wp-content/plugins');
    $url_postfix_prospect = dirname(__FILE__);
    
    if( $themes_search_index !== false ){
        $url_postfix_index = strpos($url_postfix_prospect,'wp-content/themes');
    } elseif( $plugins_search_index !== false ) {
        $url_postfix_index = strpos($url_postfix_prospect,'wp-content/plugins');
    }
    
    $url_prefix = get_site_url();
    $url_postfix = substr($url_postfix_prospect, $url_postfix_index);
    $lib_path_url = $url_prefix .'/'. $url_postfix;
    
    echo $lib_path_url;
    

    Given the file path…

    /var/www/html/example.com/wp-content/plugins/my-plugin/lib/an-awesome-library/autoload.php
    

    Results to…

    http://example.com/wp-content/plugins/my-plugin/lib/an-awesome-library
    

    Problem

    Building a plug-and-play library for wordpress that can either be “included” inside a theme or plugin can be difficult. WordPress has no pre-defined function for getting the URL of that file.

    Solution

    Treat plugins and themes separately.

    Step 1. Check whether included inside theme or plugin

    /**
     * Contains numeric values if the respective strings are found,
     * Contains boolean `false` if the string is not found
     */
    $themes_search_index = strpos(__FILE__,'wp-content/themes');
    $plugins_search_index = strpos(__FILE__,'wp-content/plugins');
    

    Step 2a. If inside a theme

    $url_postfix_prospect = dirname(__FILE__);
    
    $url_postfix_index = strpos($url_postfix_prospect,'wp-content/themes');
    $url_prefix = get_site_url();
    $url_postfix = substr($url_postfix_prospect, $url_postfix_index);
    $lib_path_url = $url_prefix .'/'. $url_postfix;
    
    echo $lib_path_url;
    

    Step 2b. If inside a plugin

    $url_postfix_prospect = dirname(__FILE__);
    
    $url_postfix_index = strpos($url_postfix_prospect,'wp-content/plugins');
    $url_prefix = get_site_url();
    $url_postfix = substr($url_postfix_prospect, $url_postfix_index);
    $lib_path_url = $url_prefix .'/'. $url_postfix;
    
    echo $lib_path_url;