0

I'm trying to get the value similar to the WordPress site url to identify a certain WordPress installation.

Since the get_site_url can get altered (with a plugin or directly) I came up with this:

untrailingslashit('http://'.$_SERVER['HTTP_HOST'].str_replace($_SERVER['DOCUMENT_ROOT'],'', ABSPATH));

This gives me good result to identify the WordPress based on it address but I'm curios if this may cause troubles on some server installations?

Xaver
  • 11,144
  • 13
  • 56
  • 91
  • I understand the value you're trying to get but my question is why? How does this help you accomplish something? There may be better ways to achieve what you're trying to achieve than the path you're already going down. – Nathan Dawson Feb 05 '14 at 14:36
  • I need to get the location (url) for licenses reasons so a certain license is used on it'S specific location – Xaver Feb 05 '14 at 14:40
  • 1
    I've added an answer which will be more reliable than using SERVER variables. Maybe consider using one of the unique keys / salts in combination with the URL so that if the plugin / theme is used on a new install it will fail validation. Be aware that there will always be a way around this. – Nathan Dawson Feb 05 '14 at 14:45

3 Answers3

2

I hope this link might help you to better understand the implications of using

home_url() vs site_url()

home_url() will only return the mapped domain on or after the init has fired. Calling it before then will return the .wordpress.com domain.

If you accidentally use site_url() in your templates, theme-side links will still redirect correctly to the home_url() equivalent.

home_url() is the preferred method, as it avoids the above redirect.
Giorgio25b
  • 406
  • 4
  • 13
  • 1
    Great explanation, you have to choose the one that fits best to your needs. – Luis Jan 22 '16 at 21:53
  • 1
    That's exactly the point Luis, it is often the case that developers are choosing functions without thinking forward to what they real needs are. – Giorgio25b Jan 24 '16 at 17:12
1

Here's my answer based on the information you've provided.

Create a function. First check if the home url is defined as a constant in wp-config. If so there's the URL. It overrides any DB value so if not set correctly the site won't function.

If that's not set then run a manual DB query to get the value of home from the options table. By running it manually instead of using get_option there's no way this value can be changed by a plugin before you use it.

function wpse_get_license_url() {
     global $wpdb;

     if ( defined( 'WP_HOME' ) ) {
         return WP_HOME;
     } else {
         $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", 'home' ) );
         return $row->option_value;
     } 
}
Nathan Dawson
  • 18,138
  • 3
  • 52
  • 58
  • I like the idea, but it's to complex imho. My solution works. I'm just not sure if it will work on all servers!? – Xaver Feb 05 '14 at 14:46
  • I've updated my answer with a simple version. If this is something you're going to access frequently I'd suggest storing the result in a transient. More info can be found here: http://codex.wordpress.org/Transients_API – Nathan Dawson Feb 05 '14 at 15:05
0
blogininfo('siteurl');
get_option('home');

home_url();
site_url();
War10ck
  • 12,387
  • 7
  • 41
  • 54
Vikas Gautam
  • 997
  • 7
  • 23