I am a big fan of the Polylang plugin as it offers a lot of functionlity in its open and free version on the WordPress repo. Recently I have been transitioning my WordPress site into the satic HTML site in order to save on hosting and reduce administartive effort. To do this, I use the Simply Static Site Generator plugin, which has a limited open and free set of functionality, but will a little creative coding one can get round some of those issues. One such issue was to force the relative links in the Polyang language switcher, by default it uses fully formed URLs and when you’re exporting your static site from your localhost, that’s an issue. Simply Static Pro plugin offers that functionality directly for both the Polyland and the WPML plugins.
To achieve this, I furst requested Google Gemini for a quick solution and after 2 attemps it led me down a rabbit hole which was a loss of time. I then asked Claude which gave me a much simpler solution, abeit it still didn’t work because it assumed that the language switcher was using anchor links, when in fact I am using the dropdown which uses a select/option element. It’s rather straight forward, and requires modifying the HTML output of the switcher using the Polylang pll_the_languages filter (documentation here),
/**
* Filter the switcher raw html to force relative links for html static export.
* @param $element html element.
* @param $args array of options for the switcher.
*/
add_filter( 'pll_the_languages', 'tiffin_language_switcher_rel_link', 10, 2);
function tiffin_language_switcher_rel_link( $element, $args ) {
$element = preg_replace_callback( '/value="([^"]+)"/',
function ( $m ) {
return 'value="' . esc_url( wp_make_link_relative( $m[1] ) ) . '"';
}, $element );
return $element;
}
It makes use of the handy WordPress core function wp_make_link_relative (documentation here).


Leave a Reply