Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Compare A Domain Name Or Hostname with A WordPress Site
*
* @author Edwin Velez
* @see https://edwinvelez.net/compare-a-domain-name-or-hostname-with-a-wordpress-site/
*/
// The domain name or host you want to compare. Can also be a host i.e. http://localhost
$live_site_url = 'https://edwinvelez.net';
$head_pattern = '/(https?:\/\/(w{3}\.)?)?(^w{3}\.)?';
$tail_pattern = '(\:[0-9]+).*|\b\/.*|\b\?.*';
$head_vs_tail_boolean = '|';
$regex_flags = '/m';
$regex_pattern = $head_pattern . $head_vs_tail_boolean . $tail_pattern . $regex_flags;
// Strip everything before and after actual domain name and hostname.
$live_site_url = preg_replace( $regex_pattern, '', strtolower( $live_site_url ) );
$site_url = preg_replace( $regex_pattern, '', strtolower( get_site_url() ) );
$is_live_site = strcmp( $site_url, $live_site_url ) === 0;
// Is the web browser pointing to the live site?
if ( $is_live_site ) {
// Insert code here to run if this is your live site.
} else {
// Insert code here to run if this is not your live site.
}
http://edwinvelez.net
http://edwinvelez.net/
https://edwinvelez.net
https://edwinvelez.net/
edwinvelez.net
www.edwinvelez.net
edwinvelez.net/testing/another?sdf=123
www.edwinvelez.net/testing/another?sdf=123
edwinvelez.net:3000/testing/another?sdf=123
www.edwinvelez.net:3000/testing/another?sdf=123
www.edwinvelez.net:3000?sdf=123
http://edwin-velez.net
https://edwin-velez.net
http://edwinvelez.net/testing/another?sdf=123
https://edwinvelez.net/testing/another?sdf=123
http://edwinvelez.net:3000/testing/another?sdf=123
https://edwinvelez.net:3000/testing/another?sdf=123
http://edwinvelez.net:3000?sdf=123
https://edwinvelez.net:3000?sdf=123
http://edwinvelez.net?sdf=123
https://edwinvelez.net?sdf=123
http://localhost
https://localhost
http://www.localhost
https://www.localhost
http://localhost:3000
https://localhost:3000
https://localhost:3000/
http://www.localhost:3000/testing/another?sdf=123
https://www.localhost:3000/testing/another?sdf=123
http://www.localhost:3000?sdf=123
https://www.localhost:3000?sdf=123
http://www.localhost.me.you:3000/testing/another?sdf=123
https://www.localhost.me.you:3000?sdf=123
http://www.localhost.me.you:3000
https://www.localhost.me.you:3000
http://www.edwinvelez.www.net
https://www.edwinvelez.www.net
http://subdomain.subdomain.www.edwinvelez.net/testing/another?sdf=123
https://subdomain.subdomain.www.edwinvelez.net/testing/another?sdf=123
http://subdomain.subdomain.www.edwinvelez.net:9871/testing/another?sdf=123
https://subdomain.subdomain.www.edwinvelez.net:9871/testing/another?sdf=123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment