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?
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?
You must be logged in to post a comment.
home_url is faster and secure than get_option(‘home’)
optionally you can append the argument using home_url function.
Both the functions will do the same thing but there is a difference in how they do it.
For instance
home_url()
will internally callget_option('home')
and add appropriate protocols like http or https. Andget_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.
Using wordpress predefined function like home_url() is much safer than getting the raw data using 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.