Skip to content

epicwp/polylang-internal-link-replacer

Repository files navigation

Polylang Internal Link Replacer

Automatically replaces internal links with their translated versions on page load for WordPress sites using Polylang.

Description

When you have a multilingual WordPress site with Polylang, internal links in your content may still point to the original language version. This plugin automatically detects and replaces these links with their translated equivalents based on the current language.

Features

  • Automatic link replacement - Replaces internal links on-the-fly during page render
  • Multiple content types - Works with post content, navigation menus, and Elementor pages
  • Smart detection - Uses WordPress rewrite rules for accurate URL resolution
  • Preserves URL components - Keeps query parameters and URL fragments intact
  • Language switcher aware - Skips links with hreflang or lang attributes
  • WooCommerce compatible - Excludes account pages to prevent redirect issues

Supported Content

  • Post and page content (the_content filter)
  • Navigation menus (wp_nav_menu_items filter)
  • Elementor frontend content (elementor/frontend/the_content filter)

Requirements

  • WordPress 6.0 or higher
  • PHP 8.1 or higher
  • Polylang (free or Pro)

Installation

Via Composer

composer require epic-wp/polylang-internal-link-replacer

Manual Installation

  1. Download or clone this repository into your wp-content/plugins/ directory
  2. Run composer install in the plugin directory
  3. Activate the plugin in WordPress admin
cd wp-content/plugins
git clone https://github.com/epicwp/polylang-internal-link-replacer.git
cd polylang-internal-link-replacer
composer install

Usage

Once activated, the plugin works automatically. No configuration needed.

The plugin runs on the frontend only and replaces internal links based on the current language set by Polylang.

Use with Polylang Automatic AI Translation

This plugin works great as a companion to Polylang Automatic AI Translation - our AI-powered translation plugin for Polylang.

The AI translation plugin already translates internal links during the translation process using intelligent link detection. However, in some edge cases, links may not be found or translated:

  • Links added after initial translation
  • Dynamic links generated by shortcodes or plugins
  • Links in cached content
  • Complex URL structures

Polylang Internal Link Replacer serves as a safety net, catching any internal links that weren't translated and replacing them on page load. This ensures your visitors always land on content in their preferred language.

How It Works

  1. When a page loads, the plugin scans the HTML content for internal links
  2. Each link URL is parsed using WordPress rewrite rules to identify the target post or term
  3. The plugin queries Polylang for the translated version in the current language
  4. If a translation exists, the link is replaced with the translated URL

Performance

The plugin is optimized for performance:

  • Only runs on frontend requests
  • Skips processing if no href attributes are found
  • Uses efficient DOM parsing with voku/simple_html_dom
  • Caches rewrite rules during request

License

GPL-2.0-or-later

Author

EPIC WP

About

Polylang Internal Link Replacer plugin

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages