使用Theme Check测试我的主题的质量,它返回我的主题是使用bloginfo();
为例:<img src="<?php bloginfo('template_url'); ?>/static/img/logo.svg"
主题检查建议我替换bloginfo()
echo esc_url( get_template_directory_uri() );
我搜索过它,但我不确定使用这个功能是不是很好。
那么,正确使用echo esc_url( get_template_directory_uri() );
来调用我主题中的任何文件?
答案 0 :(得分:3)
bloginfo('template_url')
调用get_bloginfo('template_url', 'display')
,该函数检索get_template_directory_uri()
的输出。
因此,直接使用get_template_directory_uri()
减少2个函数调用。
我不知道在这里使用esc_url()
是否有意义。函数get_template_directory_uri()
对清理网址
$template = str_replace( '%2F', '/', rawurlencode( get_template() ) );
来源:get_template_directory_uri()
在来自_s(wordpress背后的公司)的startertheme Automatic中,他们直接使用get_template_directory_uri()
而没有esc_url()
。
请看这里:functions.php
我的建议:
<img src="<?php echo get_template_directory_uri(); ?>/static/img/logo.svg"
答案 1 :(得分:1)
bloginfo('template_url')
内部使用get_template_directory_uri()
。
因此,最好使用get_template_directory_uri()
来访问theme文件夹中的文件。
使用esc_rul清理URL是一个好习惯。
在清理URL时始终使用esc_url(在文本节点中,属性 节点或其他任何地方)。拒绝没有其中一个的URL 提供白名单协议(默认为http,https,ftp,ftps, mailto,news,irc,gopher,nntp,feed和telnet),消除无效 字符,并删除危险的字符。
请参阅以下链接了解更多详情: