Skip to content

Instantly share code, notes, and snippets.

@artipixel
Last active August 6, 2017 12:51
Show Gist options
  • Save artipixel/39fa2e3fb93b190defb7699f30eeec52 to your computer and use it in GitHub Desktop.
Save artipixel/39fa2e3fb93b190defb7699f30eeec52 to your computer and use it in GitHub Desktop.
Wordpress simple domain replace [php file] - just load this file under wordpress installation.
<?php
require_once __DIR__."/wp-config.php";
global $table_prefix;
// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo "Connected successfuly to <strong>".DB_NAME.'</strong> on server <strong>'.DB_HOST.'</strong> <hr>';
}
// prepare and bind
$site_url = $conn->query("SELECT `option_value` FROM `".$table_prefix."options` WHERE `option_name`='siteurl'")->fetch_assoc();
if(sizeof($site_url) == 0){
die("Unable to find old site url. Please check your DB is configured right.");
}else{
$site_url = $site_url['option_value'];
}
if(!empty($_GET['new_url']) ){
$new_url = urldecode($_GET['new_url']);
$conn->query("UPDATE ".$table_prefix."options SET option_value = replace(option_value, '".$site_url."', '".$new_url."') WHERE option_name = 'home' OR option_name = 'siteurl'");
$conn->query("UPDATE ".$table_prefix."posts SET guid = replace(guid, '".$site_url."','".$new_url."'");
$conn->query("UPDATE ".$table_prefix."posts SET post_content = replace(post_content, '".$site_url."', '".$new_url."'");
$conn->query("UPDATE ".$table_prefix."postmeta SET meta_value = replace(meta_value, '".$site_url."', '".$new_url."'");
echo "<h1>Successfuly replaced with {$new_url}</h1>";
$conn->close();
die;
}
$generated_url = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://'."{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$generated_url = preg_replace('~/[^/]*/([^/]*)$~', '/\1', $generated_url);
$generated_url = preg_replace('{/$}', '', $generated_url);
?>
<style>
input{
width:400px;
}
</style>
<form action="" method="get">
<table>
<tr>
<td>New url:</td>
<td><input type="text" name="new_url" placeholder="New url" value="<?=$generated_url?>"></td>
</tr>
<tr>
<td>Old url:</td>
<td><input type="text" disabled style="background:#f1f1f1; color:red" value="<?php echo $site_url?>"</td>
</tr>
<tr>
<td colspan="2" >
<br>
<div style="color:red">Warning: This is irreversible!!!</p>
<input type="submit" value="Replace old URL in new URL" style="text-align: center; background:red;color:white;">
</td>
</tr>
</table>
</form>
<?php
$conn->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment