WordPress get domain

There are times when you only want to use your domain to show in your footer, for example, or use it as a dynamic logo.

In order to obtain the domain you need your site url and then retrieve the protocols here. You can do this in the following ways:

function get_domain() {
   $protocols = array( 'http://', 'https://', 'http://www.', 'https://www.', 'www.' );
   return str_replace( $protocols, '', site_url() );

The reason I use site_url() and not home_url() can you read in this article:

Difference between site url and home url

I'm Robbert Vermeulen. I document everything I learn and help thousands of people with coding WordPress. My site has no ads or sponsors. If you enjoy my content, please consider supporting what I do.


    No comments yet..

Leave a reply

Feel free to ask questions or make comments.