Compare commits

...

2 Commits

Author SHA1 Message Date
Anthony 77a645bb04 wp config edits 2025-08-08 01:36:01 +08:00
Anthony 945b9b57fc Add https on wp-config.php 2025-08-07 17:52:00 +08:00
1 changed files with 25 additions and 2 deletions

View File

@ -217,10 +217,33 @@ success "All search-replace tasks completed successfully."
CONFIG_FILE="$WP_ROOT/wp-config.php"
if [[ -f "$CONFIG_FILE" && -w "$CONFIG_FILE" ]]; then
info "Updating WP_HOME and WP_SITEURL in wp-config.php …"
# Use the new URL without trailing slash
# Use the new URL without trailing slash, ensure it has https://
TARGET_URL="$NEW_NORMAL"
# Ensure URL starts with https://
if [[ ! "$TARGET_URL" =~ ^https:// ]]; then
TARGET_URL="https://$TARGET_URL"
info "Added https:// prefix to URL: $TARGET_URL"
fi
# shellcheck disable=SC2016 # we want literal single quotes inside sed replacement
sed -i -E "s|define\(\s*'WP_HOME',\s*'[^']*'\s*\);|define( 'WP_HOME', '${TARGET_URL}' );|; s|define\(\s*'WP_SITEURL',\s*'[^']*'\s*\);|define( 'WP_SITEURL', '${TARGET_URL}' );|" "$CONFIG_FILE" || warning "Failed to update wp-config.php"
# Replace existing definitions if they exist
sed -i -E "s|define\(\s*'WP_HOME'\s*,\s*'[^']*'\s*\);|define( 'WP_HOME', '${TARGET_URL}' );|; s|define\(\s*'WP_SITEURL'\s*,\s*'[^']*'\s*\);|define( 'WP_SITEURL', '${TARGET_URL}' );|; s|\$_SERVER\['HTTP_HOST'\]\s*=\s*'[^']*';|\$_SERVER['HTTP_HOST'] = '${HOST_ONLY}';|" "$CONFIG_FILE" || warning "Failed to update wp-config.php"
# If WP_HOME wasn't present, insert block above /* That's all */
if ! grep -q "WP_HOME" "$CONFIG_FILE"; then
info "Inserting WP_HOME and WP_SITEURL constants into wp-config.php …"
awk -v url="$TARGET_URL" -v host="$HOST_ONLY" '
BEGIN{inserted=0}
/\/\* That\x27s all/{
print "\n// Added by wp-search-replace.sh";
print "define( \"WP_HOME\", \"" url "\" );";
print "define( \"WP_SITEURL\", \"" url "\" );";
print "if ( defined( \"WP_CLI\" ) && WP_CLI && ! isset( $_SERVER[\"HTTP_HOST\"] ) ) { $_SERVER[\"HTTP_HOST\"] = \"" host "\"; }";
inserted=1;
}
{print}
END{if(!inserted) exit 1}
' "$CONFIG_FILE" > "$CONFIG_FILE.tmp" && mv "$CONFIG_FILE.tmp" "$CONFIG_FILE"
fi
else
warning "wp-config.php not found or not writable; skipped WP_HOME / WP_SITEURL update."
fi