Translating a WordPress site can be a challenge, especially when the automatic translation of a page requires someone to visit it first. As a WordPress-specialist web host and WordPress-specialist webmaster, I regularly work on multilingual sites with automatic translation. For this, I usually use TranslatePress Developer edition, with the DeepL API doing the translating. The problem? Translating everything at once isn’t possible natively. So I had to find a solution. Discover it and save yourself hours!
Problem: Translating the entire site at once isn’t possible.
With TranslatePress, the translation of your pages’ and posts’ content, and especially of their URLs (available only in the paid version), only happens once someone has visited the page.
But we want all URLs to be up to date, to prevent them from changing for Google and causing 301 redirects, or even worse, 404 errors.
On small sites, you can visit every page and post in every language.
For a site with hundreds of pages and posts, visiting everything by hand would take hours.
So what can you do?
Solution: Visit the whole sitemap automatically!
The solution is this: create a script that visits every page of your site, relying on its sitemap.
Your site doesn’t have a sitemap? Click here for more details.
A sitemap is a page, usually in XML format, that you can submit to search engines (typically Google Search Console) to make it easier for them to index all the pages of your site. By today’s standards, it’s pretty much essential.
If you don’t have a sitemap yet, you can easily add one with, for example, the WordPress plugin RankMath SEO.
Next, you’ll need a terminal that supports BASH. On Linux and Mac, it’s built in. On Windows, PowerShell may support BASH, but I’d recommend installing WSL instead. If you have SSH access to a Linux server, that works perfectly too, of course.
The script
To save time, I used ChatGPT’s help to write the following script. It recursively visits all the URLs in your sitemap.xml via curl.
You can name this script, for example, “sitemap_visitor.sh”, make it executable (chmod +x sitemap_visitor.sh), then run it with your sitemap.
The two arguments to provide are:
- Your site’s URL
- The wait time between each request (you can set it to 0 if you trust your server, and your translation API usage)
For example:
./sitemap_visitor.sh https://wwww.votresite.fr/sitemap_index.xml 1
The script:
#!/bin/bash
sitemap_url="$1"
delay="$2"
declare -A visited_sitemaps # Declare an associative array to track visited sitemaps
# Function to visit all URLs in a sitemap
visit_sitemap_urls() {
local current_sitemap="$1"
# Check if the sitemap has already been visited
if [[ ${visited_sitemaps["$current_sitemap"]} ]]; then
echo "Skipping already visited sitemap: $current_sitemap"
return
fi
# Mark the current sitemap as visited
visited_sitemaps["$current_sitemap"]=1
# Fetch the sitemap
echo "Fetching sitemap from: $current_sitemap"
sitemap_content=$(curl -s -L "$current_sitemap") # Added -L to follow redirects
if [[ -z "$sitemap_content" ]]; then
echo "Failed to fetch sitemap. Skipping."
return
fi
# Extract URLs from the sitemap using grep and sed
urls=$(echo "$sitemap_content" | grep -oP '(?<=<loc>).*?(?=</loc>)')
if [[ -z "$urls" ]]; then
echo "No URLs found in the sitemap. Skipping."
return
fi
echo "Found $(echo "$urls" | wc -l) URLs in the sitemap."
# Visit each URL
while read -r url; do
echo "Visiting: $url"
response_code=$(curl -o /dev/null -s -w "%{http_code}" -L "$url") # Added -L here too
if [[ "$response_code" == "200" ]]; then
echo "Successfully visited: $url"
else
echo "Failed to visit $url: HTTP $response_code"
fi
# Respectful crawling: wait between requests
sleep "$delay"
# Check if the URL is another sitemap
if [[ "$url" == *.xml ]]; then
echo "Found nested sitemap: $url"
visit_sitemap_urls "$url"
fi
done <<< "$urls"
}
visit_sitemap_urls "$sitemap_url"
You’ve now translated your entire WordPress site!
I hope this trick saves you hours!
For a site of about a hundred pages and 5 additional languages, it cost me a little under €20 in DeepL API fees (+€4.99 for the subscription). Your experience may vary, so be sure to set limits, either in TranslatePress or in DeepL.

PS: This solution also lets you rebuild the cache for your entire site after it has been cleared. 😜
Already a LRob client and want to translate your site?
Looking for a skilled web host committed to WordPress?
Or a webmaster?
Choose LRob!









Leave a Reply
You must be logged in to post a comment.