Automating the Download and Setup of Multiple WordPress Versions in Specific Directories
When managing a WordPress project, especially during development, you may need to test multiple versions of WordPress. Instead of manually downloading, extracting, and setting up each version, this process can be automated with a simple Bash script. The script described below automates the download of various WordPress versions and places them into specific directories, making it highly efficient for testing or deployment purposes.
Overview of the Script
The Bash script you’re about to see is designed to:
- Download multiple versions of WordPress from the official WordPress.org release archive.
- Extract each version into a unique directory.
- Create separate folders for each WordPress version for easy organization and deployment.
Key Features
- Download any number of WordPress versions.
- Place each version in a specifically named directory.
- Use the flexibility of variables and functions to automate the process.
Code Walkthrough
Here is the script that automates the downloading and setup of WordPress versions in specified directories:
#!/bin/bash
# List of WordPress versions to download
versions=("5.6" "5.7" "5.8" "5.9")
# Function to download and extract each version
function download_wp {
version=$1
target_dir=$2
# Create the directory for this version
mkdir -p "$target_dir"
# Download the WordPress version
wget "https://wordpress.org/wordpress-$version.tar.gz" -O "$target_dir/wordpress-$version.tar.gz"
# Extract the downloaded tar file
tar -xzf "$target_dir/wordpress-$version.tar.gz" -C "$target_dir"
# Remove the tar file to save space
rm "$target_dir/wordpress-$version.tar.gz"
echo "WordPress $version has been downloaded and extracted to $target_dir"
}
# Loop through each version and call the download function
for version in "${versions[@]}"; do
# Define the directory for each version
target_directory="wordpress_$version"
# Call the download function
download_wp "$version" "$target_directory"
done
echo "All specified WordPress versions have been downloaded."
Detailed Explanation
- Defining WordPress Versions:
versions=("5.6" "5.7" "5.8" "5.9")
- In this line, the array
versionscontains a list of WordPress versions you want to download. You can easily modify this list to include any other versions, such as"6.0"or"6.1".
- Creating a Function to Download and Extract:
function download_wp {
version=$1
target_dir=$2
- This function
download_wpaccepts two arguments: the version of WordPress to download and the target directory where the files will be extracted. $1refers to the first argument (WordPress version), and$2refers to the second argument (target directory).
- Creating a Directory:
mkdir -p "$target_dir"
- This ensures that the specified directory is created if it doesn’t already exist. The
-poption makes it safe to run this command even if the directory already exists, avoiding errors.
- Downloading WordPress:
wget "https://wordpress.org/wordpress-$version.tar.gz" -O "$target_dir/wordpress-$version.tar.gz"
- The script uses the
wgetcommand to download the specified version of WordPress. The URL format"https://wordpress.org/wordpress-$version.tar.gz"is where the official WordPress versions are hosted. - The
-Ooption tellswgetto save the downloaded file in the$target_dirdirectory with the appropriate name.
- Extracting the WordPress Tar File:
tar -xzf "$target_dir/wordpress-$version.tar.gz" -C "$target_dir"
- After downloading, the script extracts the WordPress tarball (
.tar.gzfile) into the target directory using thetarcommand with the-xzfoptions:x: Extract the file.z: Process a gzip-compressed file.f: Specify the filename of the archive to extract.
- The
-Coption specifies the target directory for extraction.
- Cleaning Up:
rm "$target_dir/wordpress-$version.tar.gz"
- To save disk space, the script deletes the tar file after it has been extracted.
- Calling the Function for Each Version:
for version in "${versions[@]}"; do
target_directory="wordpress_$version"
download_wp "$version" "$target_directory"
done
- The script loops through each version in the
versionsarray and calls thedownload_wpfunction. For each iteration, it defines a unique directory (wordpress_$version) and passes the version number and directory to the function.
Customizing the Script
- Adding More Versions:
You can easily add or remove versions by modifying theversionsarray. For example:
versions=("4.9" "5.0" "6.0")
This will download WordPress versions 4.9, 5.0, and 6.0.
- Changing Directory Names:
The directory names are based on the version (e.g.,wordpress_5.6). You can modify thetarget_directoryvariable to fit your naming convention.
Running the Script
- Make the Script Executable:
To run the script, first, make sure it’s executable:
chmod +x download_wp.sh
- Execute the Script:
Run the script from your terminal:
./download_wp.sh
Conclusion
This Bash script offers a simple and automated way to download and set up multiple WordPress versions in specific directories. It is particularly useful for developers who need to test different versions of WordPress or manage multiple environments. By using functions, loops, and variables, the script is efficient, reusable, and customizable.
You can expand this script further by adding more features, such as automatically configuring WordPress settings, downloading plugins, or setting up databases.
Let me know if you would like to include any additional details or further improvements!
Code Summary
#!/bin/bash
# List of WordPress versions to download
versions=("5.6" "5.7" "5.8" "5.9")
# Function to download and extract each version
function download_wp {
version=$1
target_dir=$2
mkdir -p "$target_dir"
wget "https://wordpress.org/wordpress-$version.tar.gz" -O "$target_dir/wordpress-$version.tar.gz"
tar -xzf "$target_dir/wordpress-$version.tar.gz" -C "$target_dir"
rm "$target_dir/wordpress-$version.tar.gz"
echo "WordPress $version has been downloaded and extracted to $target_dir"
}
# Loop through each version and call the download function
for version in "${versions[@]}"; do
target_directory="wordpress_$version"
download_wp "$version" "$target_directory"
done
echo "All specified WordPress versions have been downloaded."

Leave a comment