Home WordPress Links Functions home_url() WordPress home_url() Function Definition In WordPress, home_url() returns the home URL of the current site. This is the address of your front page, such as https://example.com.
You use it to build links to your own pages in themes and plugins. Do not type the domain by hand. If you ever move the site, home_url() keeps your links correct. It is the short form of get_home_url() .
Syntax Return the URL and print it with esc_url().
<?php echo esc_url( home_url( '/' ) ); ?> Call What it does home_url()The home URL, without a trailing slash. home_url( '/' )The home URL with a slash. Good for the logo link. home_url( '/contact/' )The URL of the contact page.
It returns the URL. Use esc_url() when you print it.
Full declaration home_url( string $path = '', string|null $scheme = null ): string Parameters Parameter Description $pathstring. A path to add after the home URL, such as /contact/. Default is empty. $schemestring|null. http, https, or relative. Default is null, which follows the current page.
Return Value Return Value Description string The home URL, with the path added when you pass one.
WordPress Examples Three short examples. Each one shows the code and what it prints or does.
1. Link the logo to the front page 2. Build a link to another page 3. Set a search form action Example 1 — Link the logo to the front page A theme header often links the logo to the home page.
<?php
echo '<a href="' . esc_url( home_url( '/' ) ) . '">My Site</a>'; <a href="https://example.com/">My Site</a> How It Works home_url( '/' ) returns the front page address.esc_url() makes it safe for HTML.The link works even if the domain changes later. Example 2 — Build a link to another page Add the path of the page as the first argument.
<?php
echo esc_url( home_url( '/contact/' ) ); https://example.com/contact/ How It Works /contact/ is added after the home URL.You get the full URL back. Prefer get_permalink() if you know the page ID. Example 3 — Set a search form action The search form should send visitors to the front of the site.
<?php
echo '<form role="search" method="get" action="' . esc_url( home_url( '/' ) ) . '">'; How It Works A search form sends the word to the home URL. WordPress then shows the search results page. Using home_url() avoids a hard-coded domain. Frequently Asked Questions What does home_url() do? It returns the home URL of your site, which is the address of the front page.
What is the difference between home_url() and site_url()? home_url() is the front of your site. site_url() is where WordPress is installed. They can be different if WordPress is in a subfolder.
Should I type my domain in the theme? No. Use home_url() so your links keep working if the domain changes.
Does it print the URL? No. It returns a string. Use echo with esc_url().
How do I get the home URL of another site on multisite? Use get_home_url() with the site ID.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion