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

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

versus

<?php echo home_url(); ?>

Is one “safer” to use? Which one should I use?

Related posts

Leave a Reply

5 comments

  1. 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.

  2. 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.

  3. 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.