Last active
February 21, 2025 09:26
-
-
Save haze83/0a8c65479b793285e00e92f561e66f8d to your computer and use it in GitHub Desktop.
WP-CLI
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 | |
# 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 |
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
# Create a new wp-cli.yml file in the root of your wp-installation | |
# @link https://wp-cli.org/ | |
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 | |
# https://support.pagely.com/hc/en-us/articles/360030991312-Regenerating-WordPress-Thumbnails-with-WP-CLI | |
# Regenerate thumbnails all | |
wp media regenerate --yes | |
# Regenerate thumbnails missing | |
wp media regenerate --only-missing | |
# Regenerate for image ids | |
wp media regenerate 123 456 | |
# Regenerate keeping old thumbnail sizes | |
wp media regenerate --skip-delete | |
# Regenerate image size | |
wp media regenerate --url=https://stage.domain.ch --yes --image_size=large | |
# Regenerate missing images for specific media-categories (OR) | |
wp media regenerate $(wp post list --field=ID --post_type=attachment --media-category=brand-mood,brand-logo,news) --skip-delete --only-missing | |
# 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/ | |
# Cron | |
wp cron event run --due-now | |
# Action scheduler | |
# @link https://actionscheduler.org/wp-cli/ | |
wp action-scheduler run | |
# Clean action scheduler | |
wp action-scheduler clean --batch-size=50 --status=complete,failed,canceled |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment