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
<?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. And get_option('home')
will return the raw value of your set URL without making any alternations.
Also, it is recommended that you use home_url()
because its more secure.
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.
使用wordpress预定义函数(如home_url())比使用get_option('home')获取原始数据更安全;
Yes to both! (Is one faster/is one more secure)
Home_url() is both faster and more secure!
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.
While home_url is a built in function that returns the properly formatted url.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.