#!/bin/bash # Imports media recursively from any directory to wordpress media BASE_DIR="sites" TEST_MODE=false # Check for --test flag if [[ "$1" == "--test" ]]; then TEST_MODE=true echo "Running in TEST mode (only first 10 files)..." else echo "Running full import..." fi # Build the list of files sorted by modification time (newest first) FILES=$(find "$BASE_DIR" -type f \( \ -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" \ -o -iname "*.ico" -o -iname "*.doc" -o -iname "*.docx" -o -iname "*.pdf" \ -o -iname "*.txt" -o -iname "*.psd" -o -iname "*.tif" -o -iname "*.mp3" \ \) -print | xargs ls -1tr) # Limit to 10 files if in test mode if $TEST_MODE; then FILES=$(echo "$FILES" | head -n 10) fi # Loop through the files echo "$FILES" | while read file; do folder=$(dirname "$file" | sed "s|$BASE_DIR||" | sed 's|^/||') echo "Importing $file (folder: $folder)" wp media import "$file" \ --title="Drupal Import: $(basename "$file") ($(dirname "$file"))" \ --path=/var/www/html --allow-root done