diff --git a/scripts/wp-search-replace.sh b/scripts/wp-search-replace.sh index 56e53de..285a2cd 100644 --- a/scripts/wp-search-replace.sh +++ b/scripts/wp-search-replace.sh @@ -249,17 +249,25 @@ sed -i -E "s|define\([[:space:]]*['\"]WP_HOME['\"][[:space:]]*,[[:space:]]*['\"] if [[ $needs_home -eq 1 || $needs_siteurl -eq 1 || $needs_http_host -eq 1 ]]; then info "Inserting missing wp-config.php directives (WP_HOME/WP_SITEURL/HTTP_HOST) …" - awk -v url="$TARGET_URL" -v host="$HOST_ONLY" -v insert_home="$needs_home" -v insert_siteurl="$needs_siteurl" -v insert_hostfix="$needs_http_host" ' - BEGIN{inserted=0} - /\/\* That\x27s all/{ - print "\n// Added by wp-search-replace.sh"; - if (insert_home == 1) print "define( \"WP_HOME\", \"" url "\" );"; - if (insert_siteurl == 1) print "define( \"WP_SITEURL\", \"" url "\" );"; - if (insert_hostfix == 1) print "if ( defined( \"WP_CLI\" ) && WP_CLI && ! isset( $_SERVER[\"HTTP_HOST\"] ) ) { $_SERVER[\"HTTP_HOST\"] = \"" host "\"; }"; - inserted=1; + INSERT_BLOCK="\n// Added by wp-search-replace.sh\n" + if [[ $needs_home -eq 1 ]]; then + INSERT_BLOCK+="define( 'WP_HOME', '${TARGET_URL}' );\n" + fi + if [[ $needs_siteurl -eq 1 ]]; then + INSERT_BLOCK+="define( 'WP_SITEURL', '${TARGET_URL}' );\n" + fi + if [[ $needs_http_host -eq 1 ]]; then + INSERT_BLOCK+="if ( defined( 'WP_CLI' ) && WP_CLI && ! isset( $_SERVER['HTTP_HOST'] ) ) { $_SERVER['HTTP_HOST'] = '${HOST_ONLY}'; }\n" + fi + + awk -v block="$INSERT_BLOCK" ' + BEGIN { inserted=0 } + /\/\* That/ { + printf "%s", block + inserted=1 } - {print} - END{if(!inserted) exit 1} + { print } + END { if (!inserted) exit 1 } ' "$CONFIG_FILE" > "$CONFIG_FILE.tmp" && mv "$CONFIG_FILE.tmp" "$CONFIG_FILE" || warning "Could not insert directives into wp-config.php" fi else