个性化阅读
专注于IT技术分析

检索模板目录URL的更好方法是什么:bloginfo(‘template_url’)或echo esc_url(get_template_directory_uri())?

使用主题检查来测试我的主题的质量, 它返回我的主题是使用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());用于呼叫主题中的任何文件?


#1


bloginfo(‘template_url’)调用get_bloginfo(‘template_url’, ‘display’), 该函数检索get_template_directory_uri()的输出。

因此, 直接使用get_template_directory_uri()将减少2个函数调用。

我不知道在这里使用esc_url()是否有意义。函数get_template_directory_uri()具有清理URL的一小部分:

$template = str_replace( '%2F', '/', rawurlencode( get_template() ) );

资料来源:get_template_directory_uri()


在Automatic公司(wordpress背后的公司)的起始主题_s中, 他们直接使用get_template_directory_uri()而不使用esc_url()。

看这里:functions.php


我的建议:

<img src =” <?php echo get_template_directory_uri();?> / static / img / logo.svg”


#2


bloginfo(‘template_url’)内部使用get_template_directory_uri()。

因此, 最好使用get_template_directory_uri()访问主题文件夹中的文件。

最好使用esc_rul清理URL。

清理URL(在文本节点, 属性节点或其他任何地方)时, 请始终使用esc_url。拒绝没有提供的白名单协议之一的URL(默认为http, https, ftp, ftps, mailto, news, irc, gopher, nntp, feed和telnet), 消除无效字符, 并删除危险字符。

请参阅以下链接以获取更多详细信息:

  • esc_url
  • get_template_directory_uri
赞(0)
未经允许不得转载:srcmini » 检索模板目录URL的更好方法是什么:bloginfo(‘template_url’)或echo esc_url(get_template_directory_uri())?

评论 抢沙发

评论前必须登录!