请注意bloginfo(\'template_url\') 是一个wrapper 对于get_template_directory_uri().
不过,它似乎并没有明确地逃避它。
最好在你的例子中避开它。
下面是两个示例,我们如何在未转义的情况下破坏HTML:
Break Example #1
add_filter( \'template_directory_uri\', function( $uri )
{
return $uri .\'">\';
} );
Break Example #2
add_filter( \'bloginfo_url\', function( $output )
{
return $output .\'">\';
} );
因此建议:
echo esc_url( get_template_directory_uri() );
比不加掩饰地使用它更安全。
这个get_template_directory_uri() 和get_stylesheet_directory_uri() 是常用的核心函数,后者具有子主题意识。
New functions in WordPress 4.7
将引入新功能,例如,使引用和覆盖父主题文件更容易
让我们看看其中两个:
新的get_theme_file_uri( $file ) 函数可感知子主题,并同时使用get_template_directory_uri() 和get_stylesheet_directory_uri() 构造正确的文件URL。
if ( empty( $file ) ) {
$url = get_stylesheet_directory_uri();
} elseif ( file_exists( get_stylesheet_directory() . \'/\' . $file ) ) {
$url = get_stylesheet_directory_uri() . \'/\' . $file;
} else {
$url = get_template_directory_uri() . \'/\' . $file;
}
在哪里
$file 是函数的输入。
让我们试着用几句话来描述上述逻辑,以便更好地理解它:
对于空文件输入,get_theme_file_uri(), 与以下内容相同:get_stylesheet_directory_uri().
如果有子主题覆盖文件,则get_stylesheet_directory_uri() . \'/\' . $file;
如果有一个子主题没有覆盖该文件(子主题中不存在),则它是父文件回退:get_template_directory_uri() . \'/\' . $file;
如果没有子主题且文件存在,则get_stylesheet_directory_uri() . \'/\' . $file;
如果没有子主题,并且文件不存在,则get_template_directory_uri() . \'/\' . $file;
新的get_parent_theme_file_uri( $file ) 另一方面,函数仅用于get_template_directory_uri():
if ( empty( $file ) ) {
$url = get_template_directory_uri();
} else {
$url = get_template_directory_uri() . \'/\' . $file;
}
顾名思义,这是在父主题内构造文件URL。
类似地,路径也有相应的函数,而不是url:get_file_directory_path() 和get_parent_file_directory_path().
我们还应该注意,如果$file 以正斜杠开始/ 或者不戴面具ltrim:
$file = ltrim( $file, \'/\' );
参见票证
#18302 了解更多信息。
Example:
因此,在WordPress 4.7+中,我们可以尝试:
<img src="<?php echo esc_url( get_theme_file_uri(\'static/img/logo.svg\') ); ?>">
或用于父主题文件位置,具体取决于需要:
<img src="<?php echo esc_url( get_parent_theme_file_uri(\'static/img/logo.svg\') ); ?>">
如果有相应的
the_* 功能也可以简化:
<img src="<?php the_theme_file_uri(\'static/img/logo.svg\') ); ?>">
<img src="<?php the_parent_theme_file_uri(\'static/img/logo.svg\') ); ?>">
其中包括逃跑,类似于
the_permalink() 从中转义筛选的输出
get_permalink().