Skip to content

Instantly share code, notes, and snippets.

@MaximeCulea
Last active January 9, 2024 19:29
Show Gist options
  • Save MaximeCulea/575e493a061359edbb12cefc3aa4c770 to your computer and use it in GitHub Desktop.
Save MaximeCulea/575e493a061359edbb12cefc3aa4c770 to your computer and use it in GitHub Desktop.
A list of WordPress WP Cli commands when into Multisite usage.
#!/usr/bin/env bash
# Related to WP CLI as single site, check following link
# @see https://gist.github.com/MaximeCulea/dbd5835a01d2b4e6a6544919db3f26a0
# All wp cli comands will be declared both (as much as possible) :
# - with xargs (not all servers support xargs)
# - more native shell syntax "for in"
# WP Crons MS
wp site list --archived=0 --deleted=0 --public=1 --field=url --url={dom.fr} | xargs -i -n1 wp cron event run --due-now --url="{}"
for url in $(wp site list --field=url --url={dom.fr}); do wp option get siteurl --url=$url; wp cron event run --due-now --url=$url; done;
# List all sites and launch a rewrite flush for each one
# @var string main_site_url : is the main domain url
# @see https://developer.wordpress.org/cli/commands/rewrite/flush/
wp site list --field=url --url={main_site_url} | xargs -n1 -I % wp rewrite flush --url=%
for url in $(wp site list --field=url --url={main_site_url}); do wp rewrite flush --url=$url; done;
# Search replace when in subdomaines with regex, but very long!
# http > https
# Args :
# - precise : for serialized data
# - all-tables : handle all tables, not only WP's natives ones
# - network : work on ms table too and network wide
# - export : as script run could be long, don't run on current DB, prefer exporting the result for later use
# - regex : say to search-replace with regex method
# - regex-flag : precise the regex flag
# - dry-run : to lunch as debug first, then relunch without to really execute
# - url : as multisite context, should precise the main site (domain)
# @var string main_site_url : is the main domain url
# @see https://developer.wordpress.org/cli/commands/search-replace/
wp search-replace '(http://[a-z-]*.{domain})' '(https://[a-z-]*.{domain})' --url={main_site_url} --precise --all-tables --network --export=srdb.sql --regex --regex-flags='m' --dry-run
# Search replace when in subdomaines
# http > https
# @see https://developer.wordpress.org/cli/commands/search-replace/
for url in $( php wp-cli.phar site list --field=domain ); do php wp-cli.phar search-replace "http://$url" "https://$url" --precise --all-tables; done
# Full MS Sites cleaning
# Clear the cache, delete trensients, flush rewrites and lunch cron
for url in $(wp site list --field=url --url={dom.fr}); do wp option get siteurl --url=$url; wp transient delete --all --url=$url; wp cache flush --url=$url; wp rewrite flush --url=$url; wp cron event run --due-now --url=$url; done;
# Install and Update all languages in a Multisite
## Get all languages from network to install translations and update for all plugins and core
## Force to strip the language as the lang contains invisible characters
for lang in $(wp core language list --field=language --network); do wp language plugin install "$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'<<<"${lang}")" --all; wp language core install "$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'<<<"${lang}")"; done;
## Force to update core languages
wp language core update
## Force to update all plugins languages
wp language plugin update --all
# Delete a super admin from MS
# 1. Need to delete the user from all sites and reassign contents to another one
# 2. Delete his super admin role
# 3. Change network admin and/or sites admin
# 4. Delete the user
# All together
for url in $(wp site list --field=url --url=<url>); do wp option get siteurl --url=$url; wp option update admin_email <new_url> --url=$url; wp user delete <user_login> --reassign=<user_id> --url=$url; done;
wp network meta update 1 admin_email <new_email>
wp super-admin remove <user_login>
wp user delete <user_login> --network --yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment