Files
Artur Moreira 1141e6d840 feat(wordpress): script to fix siteurl/home in wp_options on CT
Adds scripts/fix-wp-siteurl-home.sh for BIT-471: updates siteurl and home
via mysqli from the wordpress container (same path as runtime). Default URL
https://newsproject.bitreport.pt; override with WP_PUBLIC_URL. Run after DB
is healthy (verify-db-connection.sh).

Made-with: Cursor
2026-03-24 19:00:59 +00:00

74 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run on the LXC from the wordpress compose directory after `docker compose up -d`.
# Sets wp_options siteurl + home to the public URL (fixes redirects to localhost:8080 when
# DB options were saved from a dev URL). Idempotent.
#
# Usage:
# bash scripts/fix-wp-siteurl-home.sh
# WP_PUBLIC_URL=https://newsproject.bitreport.pt bash scripts/fix-wp-siteurl-home.sh
#
# Prerequisites: wordpress/.env with MYSQL_PASSWORD; db healthy — run scripts/verify-db-connection.sh first if unsure.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR/.."
if [ ! -f docker-compose.yml ]; then
echo "ERROR: docker-compose.yml not found (expected at $PWD)." >&2
exit 1
fi
_URL="${WP_PUBLIC_URL:-https://newsproject.bitreport.pt}"
_URL="${_URL%/}"
echo "== fix-wp-siteurl-home: setting siteurl + home to ${_URL} (via wordpress container mysqli) =="
docker compose exec -T -e FIX_WP_PUBLIC_URL="$_URL" wordpress php -r '
$_url = getenv("FIX_WP_PUBLIC_URL");
if ($_url === false || $_url === "") {
fwrite(STDERR, "FIX_WP_PUBLIC_URL missing\n");
exit(1);
}
$_prefix = getenv("WORDPRESS_TABLE_PREFIX");
if ($_prefix === false || $_prefix === "") {
$_prefix = "wp_";
}
if (!preg_match("/^[a-zA-Z0-9_]+_$/", $_prefix)) {
fwrite(STDERR, "WORDPRESS_TABLE_PREFIX invalid\n");
exit(1);
}
$_table = $_prefix . "options";
$_password = getenv("WORDPRESS_DB_PASSWORD");
if ($_password === false || $_password === "") {
fwrite(STDERR, "WORDPRESS_DB_PASSWORD empty\n");
exit(1);
}
$_mysqli = mysqli_connect("db", "wordpress", $_password, "wordpress", 3306);
if (!$_mysqli) {
fwrite(STDERR, "mysqli_connect: " . mysqli_connect_error() . "\n");
exit(1);
}
$_stmt = $_mysqli->prepare("UPDATE `{$_table}` SET option_value = ? WHERE option_name IN (\"siteurl\", \"home\")");
if (!$_stmt) {
fwrite(STDERR, "prepare failed: " . mysqli_error($_mysqli) . "\n");
exit(1);
}
$_stmt->bind_param("s", $_url);
if (!$_stmt->execute()) {
fwrite(STDERR, "execute failed: " . $_stmt->error . "\n");
exit(1);
}
echo "rows affected: " . $_stmt->affected_rows . "\n";
$_stmt->close();
$_res = mysqli_query($_mysqli, "SELECT option_name, option_value FROM `{$_table}` WHERE option_name IN (\"siteurl\", \"home\")");
if ($_res) {
while ($_row = mysqli_fetch_assoc($_res)) {
echo $_row["option_name"] . " = " . $_row["option_value"] . "\n";
}
mysqli_free_result($_res);
}
mysqli_close($_mysqli);
'
echo "== done. Align .env WORDPRESS_PUBLIC_URL with this URL; then: docker compose up -d --force-recreate wordpress =="