Skip to content

Instantly share code, notes, and snippets.

@rmpel
Created June 3, 2025 13:45
Show Gist options
  • Save rmpel/1d376be41ceb9548bdf65fd616c79cc2 to your computer and use it in GitHub Desktop.
Save rmpel/1d376be41ceb9548bdf65fd616c79cc2 to your computer and use it in GitHub Desktop.
Convert a stand-alone WordPress site to a multisite without using the MS Setup (and therefore bypassing the disable plugins restriction)
#!/usr/bin/env bash
# Convert a stand-alone WordPress site to a multisite without using the MS Setup.
# The Set-Up wizard requires you to deactivate all plugins, but you might not want to do that.
# This script helps.
#
# NOTE: No warranty, use at own risk. Has no checks for existing multisite, it assumes this is a single site installation
# This script ASSUMES YOU KNOW WHAT YOU ARE DOING, and so do I.
CTM_DB_PREFIX="$1"
CTM_DOMAIN="$2"
CTM_ADMIN_USER="$3"
if [ -z $3 ]; then
echo "No parameters given, trying to autodetect..." >&2
CTM_IS_WP=$( wp eval 'global $table_prefix; echo $table_prefix;' >/dev/null 2>&1 && echo "YES" || echo "NO" )
if [ "YES" = $CTM_IS_WP ]; then
CTM_DB_PREFIX=$( wp eval 'global $table_prefix; echo $table_prefix;' );
CTM_DOMAIN=$( wp eval '$home = get_option("home"); print wp_parse_url($home, PHP_URL_HOST);' );
CTM_ADMIN_USER=$( wp user list --role=administrator --orderby=ID --field=user_login | head -n1 )
fi
fi
echo "-- SQL to convert to multisite: "
echo "-- - Table prefix: "$CTM_DB_PREFIX
echo "-- - Website domain: "$CTM_DOMAIN
echo "-- - Admin user: "$CTM_ADMIN_USER
echo "-- command:" $(basename $0) $CTM_DB_PREFIX $CTM_DOMAIN $CTM_ADMIN_USER
echo "-- for additional super-admin users; wp super-admin add theusername"
if [ "YES" = $CTM_IS_WP ]; then
echo "-- You can auto-execute this by piping this script to \`wp db query\`"
echo "-- for example:" $(basename $0) $CTM_DB_PREFIX $CTM_DOMAIN $CTM_ADMIN_USER "| wp db query"
fi
echo "set SQL_MODE='';"
echo "CREATE TABLE \`${CTM_DB_PREFIX}site\` ( \`id\` bigint NOT NULL AUTO_INCREMENT, \`domain\` varchar(200) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '', \`path\` varchar(100) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '', PRIMARY KEY (\`id\`), KEY \`domain\` (\`domain\`(140),\`path\`(51)));"
echo "CREATE TABLE \`${CTM_DB_PREFIX}sitemeta\` ( \`meta_id\` bigint NOT NULL AUTO_INCREMENT, \`site_id\` bigint NOT NULL DEFAULT '0', \`meta_key\` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL, \`meta_value\` longtext COLLATE utf8mb4_general_ci, PRIMARY KEY (\`meta_id\`), KEY \`meta_key\` (\`meta_key\`(191)), KEY \`site_id\` (\`site_id\`));"
echo "CREATE TABLE \`${CTM_DB_PREFIX}blogs\` ( \`blog_id\` bigint NOT NULL AUTO_INCREMENT, \`site_id\` bigint NOT NULL DEFAULT '0', \`domain\` varchar(200) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '', \`path\` varchar(100) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '', \`registered\` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', \`last_updated\` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', \`public\` tinyint NOT NULL DEFAULT '1', \`archived\` tinyint NOT NULL DEFAULT '0', \`mature\` tinyint NOT NULL DEFAULT '0', \`spam\` tinyint NOT NULL DEFAULT '0', \`deleted\` tinyint NOT NULL DEFAULT '0', \`lang_id\` int NOT NULL DEFAULT '0', \`menu_order\` int DEFAULT '0', PRIMARY KEY (\`blog_id\`), KEY \`domain\` (\`domain\`(50),\`path\`(5)), KEY \`lang_id\` (\`lang_id\`));"
echo ""
echo "INSERT INTO \`${CTM_DB_PREFIX}site\` (\`id\`, \`domain\`, \`path\`)"
echo "VALUES"
echo " (1, '${CTM_DOMAIN}', '/');"
echo ""
echo ""
echo "INSERT INTO \`${CTM_DB_PREFIX}blogs\` (\`blog_id\`, \`site_id\`, \`domain\`, \`path\`, \`registered\`, \`last_updated\`, \`public\`, \`archived\`, \`mature\`, \`spam\`, \`deleted\`, \`lang_id\`, \`menu_order\`)"
echo "VALUES"
echo " (1, 1, '${CTM_DOMAIN}', '/', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 1, 0, 0, 0, 0, 0, 0);"
echo ""
echo "INSERT INTO \`${CTM_DB_PREFIX}sitemeta\` (\`meta_id\`, \`site_id\`, \`meta_key\`, \`meta_value\`)"
echo "VALUES"
echo " (NULL, 1, 'site_admins', 'a:2:{i:0;s:5:\"admin\";i:1;s:${#CTM_ADMIN_USER}:\"${CTM_ADMIN_USER}\";}');"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment