get_locale_stylesheet_uri()
概述
get_locale_stylesheet_uri() 函数用于获取当前主题的本地化样式表 URI。它根据当前语言环境或文本方向查找对应的 CSS 文件,并支持通过过滤器修改 URI。
关键要点
- 函数返回当前主题的本地化样式表 URI,优先查找以语言环境命名的 CSS 文件(如 'zh_CN.css'),若不存在则检查文本方向文件(如 'ltr.css')。
- 可通过 'stylesheet_directory_uri' 或 'locale_stylesheet_uri' 过滤器自定义样式表目录或 URI,前者影响整个 WordPress 工作流,后者仅针对本地化文件夹。
- 函数内部使用 get_stylesheet_directory_uri()、get_stylesheet_directory() 和 get_locale() 等辅助函数,并应用 'locale_stylesheet_uri' 过滤器。
代码示例
function get_locale_stylesheet_uri() {
global $wp_locale;
$stylesheet_dir_uri = get_stylesheet_directory_uri();
$dir = get_stylesheet_directory();
$locale = get_locale();
if ( file_exists( "$dir/$locale.css" ) ) {
$stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
} elseif ( ! empty( $wp_locale->text_direction ) && file_exists( "$dir/{$wp_locale->text_direction}.css" ) ) {
$stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
} else {
$stylesheet_uri = '';
}
return apply_filters( 'locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
} Retrieves the localized stylesheet URI.
Description
The stylesheet directory for the localized stylesheet files are located, by default, in the base theme directory. The name of the locale file will be the locale followed by ‘.css’. If that does not exist, then the text direction stylesheet will be checked for existence, for example ‘ltr.css’.
The theme may change the location of the stylesheet directory by either using the ‘stylesheet_directory_uri’ or ‘locale_stylesheet_uri’ filters.
If you want to change the location of the stylesheet files for the entire WordPress workflow, then change the former. If you just have the locale in a separate folder, then change the latter.
Source
function get_locale_stylesheet_uri() {
global $wp_locale;
$stylesheet_dir_uri = get_stylesheet_directory_uri();
$dir = get_stylesheet_directory();
$locale = get_locale();
if ( file_exists( "$dir/$locale.css" ) ) {
$stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
} elseif ( ! empty( $wp_locale->text_direction ) && file_exists( "$dir/{$wp_locale->text_direction}.css" ) ) {
$stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
} else {
$stylesheet_uri = '';
}
/**
* Filters the localized stylesheet URI.
*
* @since 2.1.0
*
* @param string $stylesheet_uri Localized stylesheet URI.
* @param string $stylesheet_dir_uri Stylesheet directory URI.
*/
return apply_filters( 'locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
}
Hooks
- apply_filters( ‘locale_stylesheet_uri’, string $stylesheet_uri, string $stylesheet_dir_uri )
-
Filters the localized stylesheet URI.
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |