Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Moderate comments in a WordPress installation with WP-CLI
#!/bin/bash
# Copyright © 2016 Bjørn Johansen
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
# Check if WP-CLI is available
if ! hash wp 2>/dev/null; then
echo "WP-CLI is not available"
exit
fi
# If WordPress isn’t installed here, we bail
if ! $(wp core is-installed --quiet); then
echo "WordPress is not installed here"
exit
fi
# Get a list of site URLs
if $(wp core is-installed --quiet --network) ; then
SITE_URLS=`wp site list --fields=url --archived=0 --deleted=0 --format=csv | sed 1d`
else
SITE_URLS=(`wp option get siteurl`)
fi
# Loop through all the sites
for SITE_URL in $SITE_URLS
do
# Loop through all unmoderated comments
for COMMENT_ROW in $(wp comment list --status=0 --fields=comment_ID,comment_post_ID --format=csv --url="$SITE_URL" | sed 1d)
do
IFS=',' read -r -a COMMENT_DATA <<< "$COMMENT_ROW"
COMMENT_ID=${COMMENT_DATA[0]}
POST_ID=${COMMENT_DATA[1]}
POST_TITLE=`wp post get $POST_ID --fields=post_title --format=csv --url="$SITE_URL" | sed 1d | sed 's/^post_title,//'`
echo "Comment to the post: $POST_TITLE"
wp comment get $COMMENT_ID --url="$SITE_URL"
echo -n "(T)rash, (S)pam, (A)pprove, (N)ext, (Q)uit? "
read action
if [ "$action" == "Q" ] ; then
echo "Quitting"
exit 0;
elif [ "$action" == "T" ] ; then
echo "Trashing"
wp comment trash $COMMENT_ID
elif [ "$action" == "S" ] ; then
echo "Marking as SPAM"
wp comment spam $COMMENT_ID
elif [ "$action" == "A" ] ; then
echo "Approving"
wp comment approve $COMMENT_ID
fi
done
done
@bjornjohansen

This comment has been minimized.

Copy link
Owner Author

commented Oct 2, 2016

I’m using this with a ~/.wp-cli/config.yml with path set. You might want to do that too, or adjust the script accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.