Skip to content

Instantly share code, notes, and snippets.

@eksiscloud
Created October 23, 2019 19:30
Show Gist options
  • Save eksiscloud/1469b3c36218e33d83c3cb79f45ff9af to your computer and use it in GitHub Desktop.
Save eksiscloud/1469b3c36218e33d83c3cb79f45ff9af to your computer and use it in GitHub Desktop.
Wordpress: Restore active widgets and locations
#!/usr/bin/env bash
#
# Restore your widgets after tried new theme
#
# https://guides.wp-bullet.com/using-wp-cli-backup-restore-wordpress-widgets/
#
# loop through widget backups
for WIDGETBACKUP in /tmp/widgets/*.txt
do
# extract filename
WIDGETFILE=$(basename ${WIDGETBACKUP})
# get filename only without extension
WIDGET="${WIDGETFILE%%.*}"
# output which widget is being updated
#echo "$WIDGET"
# show widget backup contents
#cat ${WIDGETBACKUP}
WIDGETTEST=$(wp option get ${WIDGET} --allow-root)
if [[ ! -z "$WIDGETTEST" ]]; then
wp option update ${WIDGET} --format=json --allow-root < ${WIDGETBACKUP}
else
wp option add ${WIDGET} --format=json --allow-root < ${WIDGETBACKUP}
fi
done
@eksiscloud
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment