Skip to content

Instantly share code, notes, and snippets.

@rafalosinski
Last active June 22, 2017 21:29
Show Gist options
  • Save rafalosinski/df94e8ca6d8d7142c662a3928b7c4ae2 to your computer and use it in GitHub Desktop.
Save rafalosinski/df94e8ca6d8d7142c662a3928b7c4ae2 to your computer and use it in GitHub Desktop.
Change WordPress URL with mysqli & php
<?php
include 'wp-config.php';
$servername = DB_HOST;
$username = DB_USER;
$password = DB_PASSWORD;
$dbname = DB_NAME;
$old_url = "";
$new_url = "";
if ( !empty($old_url) && !empty($new_url) ) {
$conn = new mysqli( $servername, $username, $password, $dbname );
if ( $conn->connect_error ) {
die( "Connection failed: " . $conn->connect_error );
}
$sql_options = "UPDATE wp_options SET option_value = replace(option_value, $old_url, $new_url) WHERE option_name = 'home' OR option_name = 'siteurl'";
$sql_guid = "UPDATE wp_posts SET guid = replace(guid, $old_url, $new_url)";
$sql_content = "UPDATE wp_posts SET post_content = replace(post_content, $old_url, $new_url)";
$sql_postmeta = "UPDATE wp_postmeta SET meta_value = replace(meta_value, $old_url, $new_url)";
$conn->query($sql_options);
$conn->query($sql_guid);
$conn->query($sql_content);
$conn->query($sql_postmeta);
$conn->close();
header("Location: $new_url");
die();
} else {
echo "New URL or OLD url is EMPTY";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment