Blog

Get the Domain Name in Wordpress

This is a helper function which will allow you to get the domain name in Wordpress both when in WEB and CLI mode:

 

function getDomain() {
    if(isset($_SERVER['HTTP_HOST'])){
        return $_SERVER['HTTP_HOST'];
    }
    
    if(function_exists('get_site_url')){
        $url = get_site_url();
        return parse_url($url,PHP_URL_HOST);
    }
    
    if(defined('BASE_PATH')){
        return basename(BASE_PATH);
    }
    
    return dirname(__DIR__);
}