Freelancer.com

Go Back   Warrior Forum - The #1 Internet Marketing Forum & Marketplace > Warrior Support Forums > Programming
Register Blogs Social Groups Advertise with usHelp Desk

Reply
LinkBack Thread Tools
Unread 14th September 2010, 01:18 PM   #1
HyperActive Warrior
War Room Member
 
Western Grizzlin''s Avatar
 
Join Date: 2009
Posts: 164
Thanks: 18
Thanked 38 Times in 10 Posts
Default How to get root domain in Wordpress

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"?
Western Grizzlin' is offline   Reply With Quote
Unread 14th September 2010, 01:44 PM   #2
Web Developer
 
Join Date: 2010
Location: Wisconsin
Posts: 322
Thanks: 2
Thanked 79 Times in 73 Posts
Default Re: How to get root domain in Wordpress

$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;

phpbbxpert is offline   Reply With Quote
Unread 14th September 2010, 02:41 PM   #3
Advanced Profit Engineer
War Room Member
 
Vincenzo Oliva's Avatar
 
Join Date: 2010
Location: Austin Texas
Posts: 639
Thanks: 14
Thanked 237 Times in 175 Posts
Default Re: How to get root domain in Wordpress

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.
Vincenzo Oliva is offline   Reply With Quote
Unread 14th September 2010, 05:41 PM   #4
HyperActive Warrior
 
Harrison Ortega's Avatar
 
Join Date: 2010
Location: Edison, New Jersey
Posts: 112
Thanks: 11
Thanked 36 Times in 33 Posts
Default Re: How to get root domain in Wordpress

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.

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.
Harrison Ortega is offline   Reply With Quote
Unread 14th September 2010, 07:36 PM   #5
HyperActive Warrior
War Room Member
 
Western Grizzlin''s Avatar
 
Join Date: 2009
Posts: 164
Thanks: 18
Thanked 38 Times in 10 Posts
Default Re: How to get root domain in Wordpress

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!
Western Grizzlin' is offline   Reply With Quote
Unread 21st March 2011, 10:23 PM   #6
Active Warrior
War Room Member
 
Jerry-Leventer's Avatar
 
Join Date: 2008
Location: Mountain View, California
Posts: 57
Thanks: 13
Thanked 18 Times in 14 Posts
Default Re: How to get root domain in Wordpress

Quote:
I did get it to work. Thanks everyone for all your input
Can you share your solution with the rest of us?
Jerry-Leventer is offline   Reply With Quote
Unread 14th March 2012, 05:14 AM   #7
Active Warrior
 
Join Date: 2011
Posts: 45
Thanks: 0
Thanked 8 Times in 7 Posts
Default Re: How to get root domain in Wordpress

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
strangerstudios is offline   Reply With Quote
Unread 14th March 2012, 07:40 AM   #8
Active Warrior
War Room Member
 
Join Date: 2010
Location: United Kingdom
Posts: 34
Thanks: 1
Thanked 7 Times in 7 Posts
Default Re: How to get root domain in Wordpress

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:

Live Track Mobile Spy - Android Spy Software
Postcode Pal™
- Geo Datasets for GB, NI, CI and the Isle of Man
vulcanscripts is offline   Reply With Quote
Unread 16th March 2012, 10:14 AM   #9
Warrior Member
 
Join Date: 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: How to get root domain in Wordpress

Hi,

$domain = get_option('siteurl');
$domain = str_replace('http ://', '', $domain);
$domain = str_replace('www', '', $domain);
$domain = strstr($domain, '/', true);
echo $domain;

otchealth is offline   Reply With Quote
Unread 9th February 2013, 08:20 PM   #10
Active Warrior
War Room Member
 
Mark Crawford's Avatar
 
Join Date: 2011
Location: Iowa, USA
Posts: 60
Thanks: 6
Thanked 17 Times in 11 Posts
Default Re: How to get root domain in Wordpress

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

Mark Crawford is offline   Reply With Quote
Reply

  Warrior Forum - The #1 Internet Marketing Forum & Marketplace > Warrior Support Forums > Programming

Bookmarks

Tags
domain, root, wordpress

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 09:49 AM.