WordPress Shell Scripts - markhowellsmead/helpers GitHub Wiki

Run multiple custom commands concurrently

From Alain Schlesser.

Note the & - this is what executes the command as a background process and returns control to the shell for the next iteration.

for ID in $(wp post list --post_type=sht_ranking --field=ID); do
    wp sht cachefill rankingcounts $ID &
done

fromlive.sh

#!/bin/bash
SSH_SERVER="****@*******"
WEBROOT="/******"
DOMAIN_LIVE="www.example.ch"
DOMAIN_DEV="www.example.test"

ssh $SSH_SERVER -A -p 2121 "cd $WEBROOT && ~/bin/wp db export - | gzip" > db.sql.gz && gunzip db.sql.gz && wp db import db.sql && wp search-replace $DOMAIN_LIVE $DOMAIN_DEV --all-tables && rm db.sql

rsync -azP -e "ssh -p 2121" $SSH_SERVER:$WEBROOT/wp-content/uploads/ ./wp-content/uploads --exclude=".DS_Store" --exclude="*.wpress" --delete
rsync -azP -e "ssh -p 2121" $SSH_SERVER:$WEBROOT/wp-content/languages/ ./wp-content/languages --exclude=".DS_Store" --delete
rsync -azP -e "ssh -p 2121" $SSH_SERVER:$WEBROOT/wp-content/plugins/ ./wp-content/plugins --exclude=".DS_Store" --exclude=shp*
rsync -azP -e "ssh -p 2121" $SSH_SERVER:$WEBROOT/wp-content/themes/ ./wp-content/themes --exclude=".DS_Store" --exclude=sht*

sh replacedomains.sh
sh herdlink.sh

wp plugin deactivate ithemes-security-pro matomo wp-health --network --uninstall

replacedomains.sh

Replace all .ch domains with the equivalent .test domains in a local multisite environment.

#!/bin/bash

# Ensure WP-CLI is installed
if ! command -v wp &> /dev/null; then
    echo "Error: WP-CLI is not installed."
    exit 1
fi

# Get all site domains from WordPress Multisite
SITES=$(wp site list --field=url)

# Iterate through each site and update the domain
for SITE in $SITES; do
    OLD_DOMAIN=$(echo "$SITE" | awk -F/ '{print $3}')
    NEW_DOMAIN="${OLD_DOMAIN/.ch/.test}"

    if [ "$OLD_DOMAIN" != "$NEW_DOMAIN" ](/markhowellsmead/helpers/wiki/-"$OLD_DOMAIN"-!=-"$NEW_DOMAIN"-); then
        echo "Updating $OLD_DOMAIN to $NEW_DOMAIN..."
        
        # Update site URL
        wp search-replace "$OLD_DOMAIN" "$NEW_DOMAIN" --all-tables --network --skip-columns=guid --precise --quiet
        
        echo "Updated: $OLD_DOMAIN → $NEW_DOMAIN"
    fi
done

echo "All domains updated."

Herd link

Create links (aliases) for all multisite domains in the current project.

#!/bin/bash

# Ensure WP-CLI is installed
if ! command -v wp &> /dev/null; then
    echo "Error: WP-CLI is not installed."
    exit 1
fi

# Get all site URLs in the network
SITES=$(wp site list --field=url)

# Output each domain prefixed with "herd link"
echo "Multisite Domains:"
for SITE in $SITES; do
    DOMAIN=$(echo "$SITE" | awk -F/ '{print $3}')
    echo "herd link $DOMAIN"
done