Created
January 9, 2025 10:26
-
-
Save lucywoodman/b9058195c7b1a018baf8da69d5387bff to your computer and use it in GitHub Desktop.
Remove legacy wordpress multisite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cd /www/ | |
# check for existing record | |
record_check=$( wp db query 'SELECT * FROM wp_sitemeta WHERE meta_key like "ms_files_rewriting";' ) | |
if [[ $record_check == "" ]]; then | |
echo "Inserting record to disable ms-files.php" | |
wp db query 'INSERT INTO wp_sitemeta (meta_id, site_id, meta_key, meta_value) VALUES (NULL, "1", "ms_files_rewriting", "0");' | |
else | |
echo "Updating existing record to disable ms-files.php" | |
wp db query 'UPDATE wp_sitemeta SET meta_value = "0" WHERE wp_sitemeta.meta_key = "ms_files_rewriting";' | |
fi | |
cd /www/wp-content/blogs.dir/ | |
mkdir -p ../uploads/sites/ | |
for site_id in */; do | |
# Verify site_id is not empty | |
if [[ $site_id == "" ]]; then | |
continue | |
fi | |
# Root site, attempt to move files | |
if [[ $site_id == "1/" ]]; then | |
echo "Top level site found. Attempting to move files however please verify /www/wp-content/blogs.dir/1/ is empty" | |
# Move any top level files or folders | |
for file in $( ls ${site_id}/files/ ); do | |
echo "moving ${site_id}$file to ../uploads/" | |
mv ${site_id}files/$file ../uploads/ | |
done | |
continue | |
fi | |
# Not root site, move to new sites location | |
rm -rf ../uploads/sites/$site_id | |
echo "moving ${site_id}files/ to ../uploads/sites/$site_id" | |
mv ${site_id}files/ ../uploads/sites/$site_id | |
# Move any top level files or folders | |
for file in $( ls $site_id ); do | |
echo "moving ${site_id}$file to ../uploads/sites/$site_id" | |
mv ${site_id}$file ../uploads/sites/$site_id | |
done | |
# Remove folder | |
rm -rf ${site_id} | |
done | |
cd /www/ | |
# Drop legacy upload paths | |
for site in $( wp site list --field=url --skip-plugins --skip-themes ); do | |
echo "Setting $site upload_path to default." | |
wp option set upload_path "" --url=$site --skip-plugins --skip-themes | |
done | |
# The main multisite url | |
root_home=$( wp option get home --skip-plugins --skip-themes ) | |
# Correct urls for each site | |
for site_id in $( wp site list --field=blog_id --skip-plugins --skip-themes ); do | |
# Skip root site | |
if [[ $site_id == "1" ]]; then | |
continue | |
fi | |
home_url=$( wp db query "SELECT option_value from wp_${site_id}_options where option_name = 'home';" --skip-column-names --batch --skip-plugins --skip-themes ) | |
# Require site to end in / | |
if [[ $home_url != *"/" ]]; then | |
home_url="${home_url}/"; | |
fi | |
if [[ $home_url == "https"* ]]; then | |
site_name=$( basename $home_url ) | |
wp search-replace ${home_url}files/ ${home_url}wp-content/uploads/sites/${site_id}/ wp_${site_id}_* --network --report-changed-only --skip-plugins --skip-themes | |
wp search-replace ${root_home}/files/ ${home_url}wp-content/uploads/sites/${site_id}/ wp_${site_id}_* --network --report-changed-only --skip-plugins --skip-themes | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment