When you need to update a term, link, or any piece of text across all forum discussions, this script is a lifesaver. It leverages the powerful wp search-replace command but scopes it specifically to the forum post type.
#!/bin/bash
# Usage: ./replace_forum_text.sh "Old Text" "New Text"
# Check if both old and new text are provided
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 \"<old_text>\" \"<new_text>\""
exit 1
fi
OLD_TEXT=$1
NEW_TEXT=$2
echo "Searching for '$OLD_TEXT' and replacing with '$NEW_TEXT' in all forum posts and replies..."
# Run a dry run first to see what will be changed
wp search-replace "$OLD_TEXT" "$NEW_TEXT" wp_posts --post_type=topic,reply --dry-run
# Ask for confirmation before proceeding
read -p "Do you want to proceed with the replacement? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Perform the actual replacement
wp search-replace "$OLD_TEXT" "$NEW_TEXT" wp_posts --post_type=topic,reply
echo "Replacement complete."
else
echo "Operation cancelled."
fi
How to Use:
-
Save it as replace_forum_text.sh.
-
Make it executable: chmod +x replace_forum_text.sh.
-
Execute with the text to find and the text to replace it with: ./replace_forum_text.sh “Old Product Name” “New Product Name”.