Which function is used to get the home URL in WordPress?

Use home_url(). It gives the address of the front of your site, and you can add a path to it:

Terminal output

$ wp eval 'echo home_url() . "\n" . home_url( "/contact/" ) . "\n" . site_url() . "\n" . get_bloginfo( "url" );'
http://example.com
http://example.com/contact/
http://example.com
http://example.com

  • home_url(): the address where visitors open the site.
  • site_url(): the address where the WordPress core files are installed. It is different from home_url() only when WordPress is in a sub folder.
  • get_bloginfo( 'url' ): same as home_url().

Always use esc_url( home_url( '/' ) ) when you print it in HTML.


Learn more about the similar topics:
Tutorials
WordPress Themes
WordPress Top Theme List
WordPress Theme Files and Functions
WordPress Template Hierarchy
WordPress Enqueue Scripts and Styles
WordPress Loop
WordPress WP_Query Class
Exercises & Assignments
Build and activate a minimal classic theme
Show the three latest posts with WP_Query
Load a stylesheet with wp_enqueue_style and a version
Show a message only on the front page
Create a child theme which changes the footer
Interview Questions & Answers
What is a WordPress theme?
What are commonly used WordPress theme functions?
Which function is used to get the home URL in WordPress?
Which are the necessary files for a WordPress theme?
What is the use of the wp_head() function in WordPress?
What is the WordPress Loop?
What is the difference between is_home() and is_front_page()?