Updating WP_ENVIRONMENT_TYPE with the WP CLI
This assumes you have ssh access configured for your machine. Click here if you need to get it set up.
When a new environment is spun up with WP Engine, WP Engine doesn’t set up the WP_ENVIRONMENT_TYPE constant in wp-config. Here’s a shell script to use with the command line and wp-cli to make it super easy to do.
ENV="$1"
TYPE="$2"
# ---- VALIDATION ----
if [ -z "$ENV" ] || [ -z "$TYPE" ]; then
echo "Usage: $0 <environment> <type>"
echo "Example: $0 staging development"
exit 1
fi
echo "---------------------------------------------"
echo " 🔐 Connecting to environment: $ENV"
echo "---------------------------------------------"
# ---- EXECUTE REMOTE COMMANDS ----
ssh "${ENV}@${ENV}.ssh.wpengine.net" bash <<EOF
echo "📁 Changing directory to: sites/${ENV}"
cd sites/${ENV} || { echo "❌ ERROR: Directory sites/${ENV} not found."; exit 1; }
echo "⚙️ Running WP-CLI to update environment type..."
wp config set WP_ENVIRONMENT_TYPE ${TYPE} \
--type=constant \
--anchor="# WP Engine Settings" \
--placement=before \
WP_EXIT_CODE=\$?
if [ \$WP_EXIT_CODE -eq 0 ]; then
echo "✅ Successfully set WP_ENVIRONMENT_TYPE to '${TYPE}'"
else
echo "❌ WP-CLI failed with exit code \$WP_EXIT_CODE"
exit \$WP_EXIT_CODE
fi
EOF
REMOTE_EXIT=$?
echo "---------------------------------------------"
if [ $REMOTE_EXIT -eq 0 ]; then
echo "🎉 Done! Remote update completed successfully."
else
echo "❌ Something went wrong (exit code $REMOTE_EXIT)"
fi
echo "---------------------------------------------"
Here’s what to do:
- Create a file like
set-env.shwith your terminaltouch set-env.sh - Open the file, paste the above into it and save it.
- Run
./{filename}.sh {wp_env_name} {staging|development}- Example:
./set-env.sh cnodev development
- Example:
- Repeat step 3 until you’re done!