#!/usr/bin/env bash # WordPress headless setup script — runs inside the wordpress container via docker exec # Usage: docker compose exec wordpress bash /scripts/setup.sh # Requires: WP-CLI available inside container (downloaded if not present) set -euo pipefail WP_URL="${WP_URL:-http://localhost:8080}" WP_TITLE="${WP_TITLE:-News Platform}" WP_ADMIN_USER="${WP_ADMIN_USER:-admin}" WP_ADMIN_EMAIL="${WP_ADMIN_EMAIL:-admin@bitreport.pt}" WP_ADMIN_PASSWORD="${WP_ADMIN_PASSWORD:?Must set WP_ADMIN_PASSWORD}" WPCLI=/usr/local/bin/wp # Download WP-CLI if not present if [ ! -f "$WPCLI" ]; then echo "Downloading WP-CLI..." curl -sS -o "$WPCLI" https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar chmod +x "$WPCLI" fi WP="$WPCLI --allow-root --path=/var/www/html" # --- WordPress core install --- if ! $WP core is-installed 2>/dev/null; then echo "Installing WordPress core..." $WP core install \ --url="$WP_URL" \ --title="$WP_TITLE" \ --admin_user="$WP_ADMIN_USER" \ --admin_email="$WP_ADMIN_EMAIL" \ --admin_password="$WP_ADMIN_PASSWORD" \ --skip-email fi # --- Headless: disable default theme frontend --- echo "Configuring headless mode..." $WP theme activate twentytwentyfour 2>/dev/null || true $WP rewrite structure '/%postname%/' --hard # Disable comments (not needed for headless news) $WP option update default_comment_status closed $WP option update default_ping_status closed # Allow REST API for all (CORS is handled at Nginx/bridge level) $WP option update blog_public 1 # --- Plugins --- echo "Installing plugins..." # Advanced Custom Fields (free version) $WP plugin install advanced-custom-fields --activate # Yoast SEO (REST meta for articles) $WP plugin install wordpress-seo --activate # JWT Authentication (for bridge if needed) $WP plugin install jwt-authentication-for-wp-rest-api --activate # Disable XML-RPC (not needed for headless) $WP plugin install disable-xml-rpc --activate # --- Sample content: Categories --- echo "Creating categories..." declare -A CATEGORIES=( ["football"]="Football" ["basketball"]="Basketball" ["formula1"]="Formula 1" ["tennis"]="Tennis" ["mma"]="MMA" ) declare -A CAT_IDS for slug in "${!CATEGORIES[@]}"; do name="${CATEGORIES[$slug]}" existing=$($WP term get category --by=slug "$slug" --field=term_id 2>/dev/null || echo "") if [ -z "$existing" ]; then id=$($WP term create category "$name" --slug="$slug" --porcelain) else id="$existing" fi CAT_IDS[$slug]="$id" echo " Category: $name (id=$id)" done # Remove default Uncategorized if possible $WP term delete category 1 2>/dev/null || true # --- Sample content: Articles --- echo "Creating sample articles..." create_post() { local title="$1" local category_slug="$2" local excerpt="$3" local cat_id="${CAT_IDS[$category_slug]}" $WP post create \ --post_title="$title" \ --post_excerpt="$excerpt" \ --post_content="
$excerpt Full article content to be replaced with real data.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.
" \ --post_status=publish \ --post_category="$cat_id" \ --porcelain } create_post "Champions League: Real Madrid vs Manchester City Preview" "football" "A blockbuster Champions League clash as Real Madrid host Manchester City at the Bernabeu." create_post "Premier League Title Race: Arsenal vs Liverpool Analysis" "football" "Both clubs are separated by just two points with six games remaining in the Premier League season." create_post "NBA Playoffs: Golden State Warriors Eliminate Denver Nuggets" "basketball" "Steph Curry dropped 42 points as the Warriors completed a stunning comeback series win." create_post "NBA Draft Prospects: Top 10 Players to Watch" "basketball" "A deep dive into the most promising prospects heading into this summer's NBA Draft." create_post "Formula 1 Monaco Grand Prix: Starting Grid and Race Preview" "formula1" "The most glamorous race on the F1 calendar promises drama as teams split strategies." create_post "Max Verstappen Extends Championship Lead with Dominant Victory" "formula1" "The reigning world champion was untouchable as he led from lights to flag." create_post "Wimbledon Preview: Can Alcaraz Defend His Title?" "tennis" "Carlos Alcaraz arrives at SW19 as the heavy favourite after his dominant clay-court season." create_post "US Open Draw Analysis: Djokovic and Swiatek in Same Half" "tennis" "The draw has set up a potential blockbuster showdown in the semi-finals." create_post "UFC 300 Main Event: Jon Jones vs Stipe Miocic Full Preview" "mma" "The most anticipated heavyweight fight in years finally arrives at UFC 300." create_post "Conor McGregor Announces Comeback Fight Date and Opponent" "mma" "The Notorious one returns to the octagon after a three-year absence." echo "" echo "=== Setup complete ===" echo "WordPress URL: $WP_URL" echo "Admin URL: $WP_URL/wp-admin/" echo "REST API: $WP_URL/wp-json/wp/v2/" echo "Categories: $WP_URL/wp-json/wp/v2/categories" echo "Posts: $WP_URL/wp-json/wp/v2/posts" echo "" echo "Credentials are stored in the vault (not in git)."