Last active
December 17, 2021 09:54
-
-
Save maugelves/40d077c1274fb8aea7ad163b70a14c7c to your computer and use it in GitHub Desktop.
Shell process for WordPress media regeneration in batches
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
# ============================================================ | |
# Shell process to regenerate WordPress thumbnails in batches | |
# Author: Mauricio Gelves <[email protected]> | |
# Twitter: @maugelves | |
# Website: maugelves.com | |
# Version: 0.1 | |
# Syntax: wp-media-regenerate-batch.sh <options> | |
# | |
# Options: -u Database username. | |
# -d Database name. | |
# -p Database password. | |
# -s <optional> Thumbnail Size: Name of the image size to regenerate. | |
# -e <optional> Pause every X iteration, by default every 10 regenerations. | |
# -t <optional> Pause time, by default every 10 seconds. | |
# ============================================================ | |
# === VARIABLES | |
# Variables for images ID's | |
ID_NEXT="" | |
ID_TO="" | |
# database credentials | |
DB_USER="" | |
DB_PASS="" | |
DB_DATABASE="" | |
# pause variables | |
PAUSE_EVERY_X_ITERATION=10 # default 10 times | |
PAUSE_TIME=10 # default 5 seconds | |
# Thumbnails variables | |
THUMB_SIZE="" | |
# Other variables | |
COUNTER=1 | |
# === END VARIABLES | |
# RETRIEVE ARGUMENTS | |
while getopts u:d:p:s:e:t: option | |
do | |
case "${option}" | |
in | |
e) PAUSE_EVERY_X_ITERATION=${OPTARG};; | |
u) DB_USER=${OPTARG};; | |
p) DB_PASS=${OPTARG};; | |
d) DB_DATABASE=${OPTARG};; | |
s) THUMB_SIZE=${OPTARG};; | |
t) PAUSE_TIME=${OPTARG};; | |
esac | |
done | |
# VALIDATE ARGUMENTS | |
if [ "$DB_USER" == "" ] || [ "$DB_PASS" == "" ] || [ "$DB_DATABASE" == "" ] | |
then | |
echo "The parameters -u, -p and -d are mandatories" | |
fi | |
# RETRIEVE THE MIN AND MAX ID FROM ALL THE MEDIAS | |
ID_NEXT=$(mysql $DB_DATABASE -u $DB_USER -p$DB_PASS -se "SELECT MIN(ID) FROM wp_posts WHERE post_type='attachment'") | |
ID_TO=$(mysql $DB_DATABASE -u $DB_USER -p$DB_PASS -se "SELECT MAX(ID) FROM wp_posts WHERE post_type='attachment'") | |
# LOOP THE IMAGES | |
while (( $ID_NEXT <= $ID_TO )) | |
do | |
# REGENERATE THE THUMBNAIL USING WP CLI | |
if [ "$THUMB_SIZE" == "" ] | |
then | |
wp media regenerate $ID_NEXT | |
else | |
wp media regenerate $ID_NEXT --image_size=$THUMB_SIZE | |
fi | |
# MANAGE THE PAUSE BETWEEN THE REGENERATIONS | |
if (( COUNTER == PAUSE_EVERY_X_ITERATION )) | |
then | |
echo "Sleeping $PAUSE_TIME seconds" | |
sleep $PAUSE_TIME | |
# RESET THE COUNTER | |
COUNTER=0 | |
else | |
# INCRESE THE COUNTER | |
COUNTER=$((COUNTER + 1)) | |
fi | |
# GET THE NEXT IMAGE ID TO REGENERATE | |
ID_NEXT=$(mysql $DB_DATABASE -u $DB_USER -p$DB_PASS -se "SELECT ID FROM wp_posts WHERE post_type='attachment' AND ID > $ID_NEXT LIMIT 1") | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment