Skip to content

Instantly share code, notes, and snippets.

@thomhines
Last active August 29, 2015 14:06
Show Gist options
  • Save thomhines/f3350204e6db2de32b7f to your computer and use it in GitHub Desktop.
Save thomhines/f3350204e6db2de32b7f to your computer and use it in GitHub Desktop.
Quick script to alter WordPress database when moving a WordPress site from one URL to another.
<?php
// -------------------------------------------------
//
// WordPress URL Auto-Updater
// https://gist.github.com/thomhines/f3350204e6db2de32b7f
//
// -------------------------------------------------
if($_POST) {
// Connect to DB
$db = new mysqli($_POST['db_server'], $_POST['db_user'], $_POST['db_pass'], $_POST['db_name']) or die("Error: " . mysqli_error($db));
// Replace old URLs in DB
$db->query("UPDATE `".$_POST['wp_prefix']."options` set `option_value` = replace(`option_value`, '".$_POST['old_url']."', '".$_POST['new_url']."');") or die("Error: " . mysqli_error($db));
$db->query("UPDATE `".$_POST['wp_prefix']."postmeta` set `meta_value` = replace(`meta_value`, '".$_POST['old_url']."', '".$_POST['new_url']."');") or die("Error: " . mysqli_error($db));
$db->query("UPDATE `".$_POST['wp_prefix']."posts` set `post_content` = replace(`post_content`, '".$_POST['old_url']."', '".$_POST['new_url']."');") or die("Error: " . mysqli_error($db));
$db->query("UPDATE `".$_POST['wp_prefix']."posts` set `guid` = replace(`guid`, '".$_POST['old_url']."', '".$_POST['new_url']."');") or die("Error: " . mysqli_error($db));
// Close DB connection
$db->close();
echo "Database update complete.<br><br>";
}
?>
<h2>WordPress URL Auto-Updater</h2>
<p>Type in your database and URL info below to update all entries of your old URL in the WordPress database.</p>
<form method="post" accept-charset="utf-8">
<label>database name: <input name="db_name"></label><br>
<label>database user: <input name="db_user"></label><br>
<label>database pass: <input name="db_pass"></label><br>
<label>WordPress table prefix: <input name="wp_prefix" value="wp_"></label><br>
<label>server: <input name="db_server" value="localhost"></label><br>
<br>
<label>old URL: <input name="old_url"></label><br>
<label>new URL: <input name="new_url"></label><br>
<input type="submit" value="Update Database">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment