After creating a WordPress website, you get a URL (Uniform Resource Locator) that people can use to visit it, but sometimes you need to show it elsewhere. Things that come to mind include the home page, Contact Us page, WordPress theme or plugin, or creating code snippets to help the site functionality. Pulling up a URL can also be handy for a variety of Admin section features, and plays a role in listing or redirecting to pages on your site. Thus, our guide focuses on options besides seeing the URL in the browser address bar. Instead, we’ll teach you how to get the site URL in WordPress in other ways.
Note. Pay attention to the lack of a trailing slash when a data path is needed, i.e., https://www.your-website.com/folder
instead of https://www.your-website.com/folder/
Also note the difference between site_url (any URL where the website is always reachable by adding /wp-admin or a variation thereof) and home_url (home page URL, not necessarily identical).
Whenever a function or coding for WordPress is mentioned, we propose users visit the WordPress Codex, specifically its Code Reference page. Since the CMS (Content Management System) is a community-driven project, WordPress developers and those aspiring to be one all gather to write extensive documentation. Thankfully, that is also the case for the primary functions:
1. Using the site_url() function on the WordPress Codex to get the site URL
This is the first function you’ll see, and likely the one you’ll use for simple projects. As the Code Reference suggests, it will output the URL of the current site if it detects it uses WordPress. It does so by checking for the wp-admin folder which can sometimes be protected, so it’s not infallible. It may also verify whether other WordPress app data such as the wp-blog-header.php file exist. As you can see in the example, a PHP code that already exists in wp-includes/link-template.php looks this way:
function site_url( $path = ”, $scheme = null ) {
return get_site_url( null, $path, $scheme );
}
The simplest example look like this:
$url = site_url();
echo $url;
That would print an output either: http://www.example.com or if the installation is moved to a separate folder, http://www.example.com/wordpress As you can note in the parameters, you can also add http or https and additional folders to the data path should the need arise, like:
$url = site_url( ‘/foldername/’, ‘https’ );
echo $url;
2. Utilize the get_site_url function on WordPress Code Reference
A similar function that has the same output yet accepts more parameters. You can input a site ID under $blog_id to output an URL of a different side, though if you skip it, it outputs the current one. You can do the same thing with the path as above, and use scheme to include “relative”, “admin”, “login_post”, “http” (not recommended), “https” or “login”. The simplest PHP code looks like this:
<?php echo get_site_url(); ?>
It will display https://www.example.com or if the CMS is installed to a subfolder, a full path such as https://www.example.com/subfolder. Users frequently need the domain name part for reference in their plugins. In that case, echo parse_url( get_site_url(), PHP_URL_HOST ) );
will work.
3. Employ base_URL and home_URL WordPress functions
Many website owners designing their themes need to reference the base or home URL. That is possible through the template tag that would retrieve the URL for the current site. In that case, they can use:
get_bloginfo('wpurl');
Alternatively, the one below is frequently used:
<?php echo get_bloginfo('url') ?>
Are you using a JavaScript file and need bloginfo baseurl
? The following simple script would do the job:
<script type="text/javascript">
var baseurl = '<?php echo get_bloginfo("url"); ?>';
var tplurl = '<?php echo get_bloginfo("template_directory"); ?>';
</script>
Another option includes a function like this <?php echo home_url(); ?>
and if a further file path is required, add it like this: <?php echo home_url('/contact'); ?>
Similarly, if your permalink structure is different, you won’t have problems using something like this:
$url = esc_url(home_url( '/site-page?id=222));
echo $url;
That would provide this output: https://your-site.com/site-page?id=222
Some users also found this code to work:
global $wp;
echo home_url( $wp->request )
That brings us to the topic of rarely needed functions such as the root directory path. That one is primarily required by theme developers and looks like this:
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');