Skip to content

Fix Polylang sitemap URLs when "hide default language" setting is disabled#727

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-675
Draft

Fix Polylang sitemap URLs when "hide default language" setting is disabled#727
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-675

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 27, 2025

This PR fixes an issue where language-specific sitemaps become inaccessible when Polylang's "The front page URL contains the language code instead of the page name or page id" setting is disabled.

Problem

When the Polylang setting "Hide URL language information for default language" is disabled, the front page URL structure changes, but TSF's sitemap endpoint generation doesn't account for this:

  • robots.txt advertises: domain.com/lang_slug/sitemap.xml
  • But actual sitemap is at: domain.com/lang_slug/page-title/sitemap.xml

This causes 404 errors when search engines try to access the sitemap URLs listed in robots.txt.

Root Cause

The _polylang_register_sitemap_languages() function was making assumptions about URL structure, always generating endpoints like {language}/sitemap.xml without considering how Polylang's settings affect the actual front page URL structure.

Solution

Updated the sitemap endpoint generation to:

  1. Use Polylang's pll_home_url() function to get the actual language-specific home URLs
  2. Dynamically calculate the relative path for each language based on the actual URL structure
  3. Generate sitemap endpoints that match where the sitemaps are actually accessible
  4. Include fallback to the original method if pll_home_url() is unavailable

Example

Before: Always generates es/sitemap.xml regardless of Polylang settings
After:

  • When "hide default" enabled: es/sitemap.xml
  • When "hide default" disabled: es/page-title/sitemap.xml

Testing

  • ✅ Handles various URL structures (subdirectories, different page titles)
  • ✅ Maintains backward compatibility with existing configurations
  • ✅ Includes proper fallback for edge cases
  • ✅ All syntax checks pass

Fixes #675.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: sybrew <7662236+sybrew@users.noreply.github.com>
Copilot AI changed the title [WIP] Sitemap issue with Polylang Fix Polylang sitemap URLs when "hide default language" setting is disabled Jun 27, 2025
Copilot AI requested a review from sybrew June 27, 2025 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sitemap issue with Polylang

2 participants