How to get root domain in Wordpress

by 9 comments
I'm working on a Wordpress plugin and I'm trying to get the domain in the following form "".

Don't want http://www or www

In Wordpress, I know you can get using get_option('home').

Does anyone have any idea how I could get ""?
#programming #domain #root #wordpress
  • Profile picture of the author phpbbxpert
    $domain = get_option('siteurl'); //or home
    $domain = str_replace('http://', '', $domain);
    $domain = str_replace('www', '', $domain); //add the . after the www if you don't want it
    $domain = strstr($domain, '/', true); //PHP5 only, this is in case WP is not root
    echo $domain;
  • Profile picture of the author Vincenzo Oliva
    It seems that you're making this more complicated than need be if I understand you correctly.
    I'd go to my wp-admin panel, settings/General settings and enter what you want the wordpress url and site url to be, done.
  • Profile picture of the author strangerstudios
    I came across this thread in a Google search. To help others who might get here trying to do this, you can use the parse_url PHP function to find the full domain and then split that up to get just the TLD and domain name.

    PHP: parse_url - Manual

    //get the full domain
    $urlparts = parse_url(site_url());
    $domain = $urlparts [host];

    This would return something like If you want to trim the subdomains off, split this up and get just the last two parts.

    //get the TLD and domain
    $domainparts = explode(".", $domain);
    $domain = $domainparts[count($domainparts)-2] . "." . $domainparts[count($domainparts)-1];

    Use all of the code above to return (without the www).

    Hope this helps.

  • Profile picture of the author otchealth

    $domain = get_option('siteurl');
    $domain = str_replace('http ://', '', $domain);
    $domain = str_replace('www', '', $domain);
    $domain = strstr($domain, '/', true);
    echo $domain;
  • Profile picture of the author Mark Crawford
    I tried the above and it did not work for me so I searched some more and found this that did work for me.

    $domain_name = preg_replace('/^www\./','',$_SERVER['SERVER_NAME']);

    hope it helps

Next Topics on Trending Feed