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__);
}

 

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form. Links to this article are welcome

Other Related Articles