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.
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 edited Mar 11, 2012 at 6:52 Dogbert asked Mar 11, 2012 at 5:14 DogbertDogbert 2795 silver badges15 bronze badges 5- Do you know the exact location within the Plugin where the file would be located? – Chip Bennett Commented Mar 15, 2012 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 Commented Mar 15, 2012 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 Commented Mar 16, 2012 at 5:01
- @EAMann It's an Options page generator/helper, that could either be used by a plugin or a theme. – Dogbert Commented Mar 16, 2012 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 Commented Mar 16, 2012 at 11:34
6 Answers
Reset to default 4See 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';
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.
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.
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', '' );
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/wp-content/plugins/my-plugin/lib/an-awesome-library/autoload.php
Results to...
http://example/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;
$path = dirname(__FILE__);
should do the trick.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744693389a4588346.html
评论列表(0条)