Skip to content

Instantly share code, notes, and snippets.

@unapersona
Created August 10, 2012 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unapersona/3312905 to your computer and use it in GitHub Desktop.
Save unapersona/3312905 to your computer and use it in GitHub Desktop.
Change Wordpress domain
<?php
/*
WP Domain Changer
(changing directly in the database)
http://codex.wordpress.org/Changing_The_Site_URL#Changing_the_URL_directly_in_the_database
Usage:
browser: http://domainname/wp-domain-changer.php
commandline: php wp-domain-changer.php http://domainname [path-to-wp-config.php]
*/
if(isset($_SERVER["HTTP_HOST"])){
$domain = 'http://'.$_SERVER["HTTP_HOST"];
}else if(isset($_SERVER['argv'][1])){
$domain = $_SERVER['argv'][1];
}else{
exit('please specify a domain name!');
}
if(strpos($domain, 'http') !== 0){
exit('domain name "'.$domain.'" is not valid!');
}
if(isset($_SERVER['argv'][2])){
if(@!include_once($_SERVER['argv'][2]))
exit($_SERVER['argv'][2].' not found!');
}else if(@!include_once('wp-config.php'))
exit('wp-config.php not found!');
try{
$dbh = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWORD);
$dbh->exec('SET CHARACTER SET '.DB_CHARSET);
$sql = 'UPDATE '.$table_prefix.'options
SET option_value="'.$domain.'"
WHERE option_name IN ("siteurl", "home")';
$n = $dbh->exec($sql);
if($n == 2) echo('domain changed succesfully!');
else if($n == 1) echo('something weird happens! please update database manually!');
else{
$e = $dbh->errorInfo();
if(isset($e[2])) echo $e[2];
else echo 'nothing to change!';
}
$dbh = null;
}catch(Exception $e){
echo 'Error: '.$e->getMessage();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment