How to get root domain in Wordpress

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

Don't want http://www or www

In Wordpress, I know you can get http://domain.com using get_option('home').

Does anyone have any idea how I could get ".domain.com"?
#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;
    {{ DiscussionBoard.errors[2597900].message }}
  • 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.
    {{ DiscussionBoard.errors[2598157].message }}
    • Profile picture of the author Harrison Ortega
      You don't need to exclude the www. With the correct basenames the plugin will follow the default scheme.

      This code will retrive the home url for the current site.
      Code:
      <?php home_url( 'path', 'scheme' ); ?>
      Other functions to retrieve specific files from other directories.

      Code:
      site_url();
      admin_url();
      content_url();
      plugins_url();
      includes_url();
      If you want the plugin to be compatible with versions below 2.6 then you need something like this:
      Code:
      if ( ! defined( 'WP_CONTENT_URL' ) )
            define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
      BTW, Path and scheme are optionals. Path, if you want to hard code the URL, and scheme if you want to hard code https.
      Signature
      NJ web design / NJ Web Designer. MY Wordpress portfolio. 10 years of HTML/CSS - 6 years developing professional Wordpress websites. Currently not available for services.
      {{ DiscussionBoard.errors[2598920].message }}
      • Profile picture of the author Western Grizzlin'
        I was looking to get the ".domain.com" for a cookie that I was setting. I read that using a domain in that form gave the least problems for the least browsers.

        I did get it to work. Thanks everyone for all your input!
        {{ DiscussionBoard.errors[2599400].message }}
        • Profile picture of the author Jerry-Leventer
          I did get it to work. Thanks everyone for all your input
          Can you share your solution with the rest of us?
          {{ DiscussionBoard.errors[3571970].message }}
  • 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 www.warriorforum.com. 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 warriorforum.com (without the www).

    Hope this helps.

    Jason
    {{ DiscussionBoard.errors[5823499].message }}
    • Profile picture of the author vulcanscripts
      I can see the advantages of setting a cookie on the top level domain itself, as it will be valid up and throughout all sub domains. However, if you're building a distributable plugin I'm afraid no universal solution for this exists, unless of course you're willing to incorporate a complete list of top and second level domains? The example given by phpbbxpert would only suffice providing that wordpress is installed under the sub domain of 'www'. It would not account for an installation on blog.domain.tld. The example given by strangerstudios above does not account for second level domains. I.e. domain.sld.tld. This would obviously impact the ten million second level domains in the UK, such as .co.uk and that's just one example; there are thousands of popular second level domains used around the world.

      Now if I was to set myself up with the challenge of building a php function (and I am) that is independent of any list, then I would use statistics and regular expression to ensure that the function is as universal as possible. It would look something like this:

      function resolve_root_domain($url, $max=6)
      {
      $domain = parse_url($url, PHP_URL_HOST);

      if (!strstr(substr($domain, 0, $max), '.'))
      return ($domain);
      else
      return (preg_replace("/^(.*?)\.(.*)$/", "$2", $domain));
      }

      This function considers that most top level domains are longer than six characters and that most sub domains precede the top level within six characters. It also considers the thousands of second level domains that are out there. The optional second parameter is for playing with the 6 character rule. It's obviously not completely universal, but it's certainly more universal than the other examples given in this post and like I said; without incorporating any lists, universal compatibility would be an impossibility. Third and fourth level domains are also fairly common, but setting a cookie on these levels will not impact on a browser a great deal in terms of wordpress.

      To set a cookie in wordpress you could do something like this:


      $domain = resolve_root_domain(get_option('siteurl'));
      setcookie('NAME', 'DATA', time()+3600, '/', '.'.$domain);

      For the benefits of global browser compatibility the 4th paremeter should always be a / if you want the cookie to be traversal, throughout the directory structure of your website and you should precede the domain name itself with a . if you want to factor in older browsers that implement the deprecated RFC 2109 standards. Without it the cookie will not be read as a wildcard that is valid throughout all sub domains.

      Coffee break over! Back to do some work. :rolleyes:
      Signature
      Live Track Mobile Spy - Android Spy Software
      Postcode Palâ„¢
      - Geo Datasets for GB, NI, CI and the Isle of Man
      {{ DiscussionBoard.errors[5824153].message }}
  • Profile picture of the author otchealth
    Hi,

    $domain = get_option('siteurl');
    $domain = str_replace('http ://', '', $domain);
    $domain = str_replace('www', '', $domain);
    $domain = strstr($domain, '/', true);
    echo $domain;
    {{ DiscussionBoard.errors[5839285].message }}
  • 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
    {{ DiscussionBoard.errors[7720038].message }}

Trending Topics