简体   繁体   English

home_url()与get_option('home')之间的区别

[英]Difference between home_url() versus get_option('home')

I need to echo out my website's URL. 我需要回应我网站的网址。 I read through WordPress Codex but I still do not understand, what is the difference between 我通过WordPress Codex阅读但我仍然不明白,有什么区别

<?php echo get_option('home'); ?>

versus

<?php echo home_url(); ?>

Is one "safer" to use? 一个“更安全”使用? Which one should I use? 我应该使用哪一个?

home_url比get_option('home')更快更安全,可选择使用home_url函数追加参数。

Both the functions will do the same thing but there is a difference in how they do it. 这两个函数都会做同样的事情,但它们的工作方式却有所不同。

For instance home_url() will internally call get_option('home') and add appropriate protocols like http or https. 例如, home_url()将在内部调用get_option('home')并添加适当的协议,如http或https。 And get_option('home') will return the raw value of your set URL without making any alternations. 并且get_option('home')将返回您设置的URL的原始值,而不进行任何get_option('home')

Also, it is recommended that you use home_url() because its more secure. 此外,建议您使用home_url()因为它更安全。

I would stick to the function. 我会坚持这个功能。 It is a helper function that not only handles the option retrieval for you but sets http/https and (optionally) appends a path so no need to do the grunt work yourself. 它是一个辅助函数,不仅可以为您处理选项检索,还可以设置http / https和(可选)附加路径,因此无需自己执行繁琐的工作。

使用wordpress预定义函数(如home_url())比使用get_option('home')获取原始数据更安全;

Yes to both! 两个都是! (Is one faster/is one more secure) (一个更快/更安全一个)

Home_url() is both faster and more secure! Home_url()更快更安全!

The answer lies in the behavior of each function you reference in the question: 答案在于您在问题中引用的每个函数的行为:

Get_option goes through a controller to find the option with the name given. Get_option通过控制器查找具有给定名称的选项。

While home_url is a built in function that returns the properly formatted url. 虽然home_url是一个内置函数,它返回格式正确的url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM