Get URL of a specific file

时间:2012-03-11 作者:Dogbert

我有一个文件,可以包含在插件中,也可以包含在主题中。(从文件中)获取文件文件夹URL的最佳方法是什么?

编辑:

我现在用这个

home_url( \'/\' . str_replace( ABSPATH, "", dirname( __FILE__ ) ) );
如果有什么问题,请告诉我/有更好的方法。

6 个回复
SO网友:fuxia

看见Determining Plugin and Content Directories.

plugins_url( \'filename\', __FILE__ );
…返回插件中文件的完整URI。对于您使用的主题:

get_stylesheet_directory_uri() . \'/path/to/file\';

SO网友:sanchothefat

使用ABSPATH和home_url() 如果wp安装位于与其显示的url不同的目录中,则可能无法工作。你应该测试一下。

我的想法是,使用内容目录作为替换的位置可能更可靠,因为您可以将生成的路径传递到content_url() 说明WP安装的位置:

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;
}
这只适用于unix,但上面也支持windows。

SO网友:nembleton

为什么不写两个案例并检查哪一个实际指向真实文件:

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 "";
    }
}
这是我所看到的最好的选择。

SO网友:EAMann

我建议你用过滤器。

在您的文件中,在最开始的某个地方,包括以下内容:

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;
}
然后,在需要引用文件的代码中,将筛选器应用于空字符串:

$include_url = apply_filters( \'wpa45165_resource_url\', \'\' );

SO网友:Abel Melquiades Callejo

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;
SO网友:Vijay Sharma

$path = dirname(__FILE__);
应该做到这一点。

结束

相关推荐

Mu-plugins文件夹在多站点安装中是如何工作的?

我对什么时候应该将插件文件放入mu插件有点困惑。我知道它必须使用插件,而且我知道它们是自动启用的。但是,在网络设置中,这是否会强制在所有子博客中启用插件文件?这是有道理的,那么这是我唯一一次应该在网络中放置插件文件吗?我什么时候希望插件代码在每个子博客中运行?例如,如果我想自动设置一个新的缩略图大小,我应该在那里这样做吗?或者还有其他例子吗。那么网络激活呢?