Coder Social home page Coder Social logo

wprobot / wordpress-auto-detect-site-url Goto Github PK

View Code? Open in Web Editor NEW

This project forked from macdonaldrobinson/wordpress-auto-detect-site-url

0.0 0.0 0.0 10 KB

The repo contains the code that you can use in your wp-config.php file to auto detect the current site URL and replace the WP_HOME and the WP_SITEURL variables

wordpress-auto-detect-site-url's Introduction

Wordpress-Auto-Detect-Site-Urls

I was getting really frustrated on how i had to constantly change the WP_HOME and the WP_SITEURL in the database every time I was switching environments for wordpress sites.

So I decided to write the below code to the wp-config.php file, which adds the ability to dynamicly switch the urls at runtime.

It also contains code showing how you can switch the DB connection string based on different hostnames / urls.

$port = ':'.$_SERVER["SERVER_PORT"];

if (isset($_SERVER) && array_key_exists('SERVER_NAME', $_SERVER)) {
  $serverName = $_SERVER['SERVER_NAME'];
} else {
  global $_FILE_TO_URL_MAPPING;
  $basePath = dirname(SOURCE_PATH);
  $urlInfo = parse_url($_FILE_TO_URL_MAPPING[$basePath]);
  $serverName = $urlInfo['host'];
}

global $databaseConfig;

if (!empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1')) {
	$protocol = 'https';
} else {
	$protocol = 'http';
}

if($port == ':80' || $protocol == 'https')
	$port = '';

$url = $protocol.'://'.$serverName.$port;

// Tells wordpress to use the auto detected urls instead of the ones in the db
define( 'WP_HOME', $url );
define( 'WP_SITEURL', $url );

switch ($serverName) {
	// LIVE SITE SETTINGS
	case 'site.com':
	case 'www.site.com':	
	//case 'localhost':
		define('DB_NAME', '[DB_NAME]');
		define('DB_USER', '[DB_USER]');
		define('DB_PASSWORD', '[DB_PASSWORD]');
		define('DB_HOST', '[DB_HOST]');		
		define('DB_CHARSET', 'utf8');
		define('DB_COLLATE', '');
		define('WP_DEBUG', false);
	break;
	// LOCAL/DEV SITE SETTINGS
	default:
		define('DB_NAME', '[DB_NAME]');
		define('DB_USER', '[DB_USER]');
		define('DB_PASSWORD', '[DB_PASSWORD]');
		define('DB_HOST', '[DB_HOST]');		
		define('DB_CHARSET', 'utf8');
		define('DB_COLLATE', '');
		define('WP_DEBUG', true);
	break;
}

wordpress-auto-detect-site-url's People

Contributors

macdonaldrobinson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.