Move 1 Site in Multisite

Posted by on Jun 19, 2017 in WordPress

Change a Site Folder Name in Multisite

Have you ever setup a WordPress Multisite, then discovered one of the site names (i.e.: folder name) was wrong?  No?  Oh.  Well, I did.  But the good news is that I fixed it!

Hey, it could happen.  Maybe you get a better idea about how those site folders should be named, but the entire multisite is already setup.

All you have to do is wave a magic wand (and run the script below).

Let me explain a few things you need to edit in the script –

Preface: This sample assumes just 1 child site.  It’s sub-prefix (after table_prefix) is 2_ And it is used 3 times for 2 tables (see lines 22 through 26.  You can change these numbers to the ones applicable to the site you wish to move.  You can then re-run the script if you’re doing multiple sites.  Just be sure to correctly match your folder names with the correct numbers.  If you are not comfortable working with databases, you should hire a professional. (Hey, I’m fast & cheap!)

  1. Change the old and newFolderName values between the quotes, to those applicable in your WordPress multisite.
  2. Copy the database settings from your wp-config.php file to replace lines 4 through 15.
  3. Further down in your wp-config.php file, find “$table_prefix” and change its value at line 16.
  4. Note that this may change email addresses or other fields that share the same text value!
  5. Maybe this should be step #1 – Backup your database!
  6. Upload this script (below) and run it.
  7. Send gratuities to hank@compusolver.com!  🙂

 

<?php
// Code by Hank Castello - CompuSolver.com
$oldFolderName = "oldname";
$newFolderName = "newname";
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'mydbname');
/** MySQL database username */
define('DB_USER', 'myusername');
/** MySQL database password */
define('DB_PASSWORD', 'lkdflk90i8');
/** MySQL hostname */
define('DB_HOST', 'localhost');

$table_prefix = 'abc_';

// Do not edit below this line
$conn = mysqli_connect( DB_HOST, DB_USER, DB_PASSWORD, DB_NAME );
if(! $conn ) {
 die('Could not connect: ' . mysqli_error($conn));
}
$sql = "UPDATE " . $table_prefix . "2_options SET option_value = REPLACE(option_value, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "2_posts SET guid = REPLACE(guid, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "2_posts SET post_content = REPLACE(post_content, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "blogs SET path = REPLACE(path, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "posts SET guid = REPLACE(guid, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "posts SET post_content = REPLACE(post_content, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "registration_log SET email = REPLACE(email, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "terms SET slug = REPLACE(slug, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "usermeta SET meta_value = REPLACE(meta_value, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "wpqr_users SET user_login = REPLACE(user_login, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "wpqr_users SET user_nicename = REPLACE(user_nicename, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "wpqr_users SET user_email = REPLACE(user_email, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);
$sql = "UPDATE " . $table_prefix . "wpqr_users SET display_name = REPLACE(display_name, $oldFolderName, $newFolderName)";
mysqli_query($conn, $sql);