Skip to content

Instantly share code, notes, and snippets.

@haze83
Last active February 12, 2024 10:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haze83/0a8c65479b793285e00e92f561e66f8d to your computer and use it in GitHub Desktop.
Save haze83/0a8c65479b793285e00e92f561e66f8d to your computer and use it in GitHub Desktop.
WP-CLI
#!/bin/bash
# bash -c "$(curl -s https://gist.githubusercontent.com/haze83/0a8c65479b793285e00e92f561e66f8d/raw/d2537e8e3db767a9eb0954446e6bde571f107dc1/create-config.sh)"
echo "WP user: "
read WP_USER
echo "PROD (no leading /, relative to $HOME): "
read WP_PATH_PROD
echo "STAGING (no leading /, relative to $HOME): "
read WP_PATH_STAGING
echo "DEV (no leading /, relative to $HOME): "
read WP_PATH_DEV
mkdir ~/.wp-cli
{
echo "user: $WP_USER"
echo "db export:"
echo " add-drop-table: true"
echo " default-character-set: utf8mb4"
echo ""
echo "search-replace:"
echo " recurse-objects: true"
echo " all-tables: true"
echo ""
echo "@prod:"
echo " path: $HOME/$WP_PATH_PROD"
echo "@staging:"
echo " path: $HOME/$WP_PATH_STAGING"
echo "@dev:"
echo " path: $HOME/$WP_PATH_DEV"
} >> ~/.wp-cli/config.yml
# Create a new wp-cli.yml file in the root of your wp-installation
touch wp-cli.yml
# Open the file to edit
nano wp-cli.yml
# Enter your installation data
path: /path/to/my/wp-root/
url: https://dev.domain.ch/
user: admin
# Press Ctrl+x and y to save and exit nano
# alternative create a global ~/.wp-cli/config.yml file
user: admin
# debug: true
db export:
add-drop-table: true
default-character-set: utf8mb4
search-replace:
recurse-objects: true
all-tables: true
@prod:
path: /path/to/www.domain.ch
@staging:
path: /path/to/staging.domain.ch
@dev:
path: /path/to/dev.domain.ch
# usage: wp @dev option get blogname
# Regenerate image size
wp media regenerate --url=https://stage.domain.ch --yes --image_size=large
# Smush all images
# for more see: https://wpmudev.com/docs/api-plugin-development/smush-api-docs/
wp smush compress
# Smush compress only missing images
for image_id in $(wp smush list | awk '{ if($1 != "ID" && $1 != "Success:") print $1; }'); do wp smush compress --type=single --image=$image_id; done
# Search old url and replace with new url
wp search-replace --url=https://stage.domain.ch https://www.domain.ch https://stage.domain.ch --skip-columns=guid --user=admin
wp search-replace www.oldurl.ch www.new-url.ch --recurse-objects --all-tables --skip-plugins --skip-themes
# Delete all WooCommerce orders
wp post delete $(wp post list --field=ID --post_type="shop_order" --post_per_page=-1) --force
# Delete all WooCommerce customers
wp user delete $(wp user list --role=customer --field=ID) --yes
# more enhanced examples for using wp-cli see:
# https://www.presslabs.com/code/wp-cli-guide-wp-term/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment